2024-07-17 10:17:25 +03:30

24 lines
862 B
Dart

// GET http://localhost:3000/p/random
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../model/my_random.dart';
// Necessary for code-generation to work
part 'getRandomData.g.dart';
/// This will create a provider named `activityProvider`
/// which will cache the result of this function.
@riverpod
Future<MyRandom> getMyRandom(GetMyRandomRef ref) async {
// Using package:http, we fetch a random activity from the Bored API.
var ur = Uri.http('192.168.160.207:3000', '/p/random');
final response = await http.get(ur);
// Using dart:convert, we then decode the JSON payload into a Map data structure.
final json = jsonDecode(response.body) as Map<String, dynamic>;
// Finally, we convert the Map into an Activity instance.
return MyRandom.fromJson(json);
}