diff --git a/lib/mains/gt.dart b/lib/mains/gt.dart index a4f8795..d4663d9 100644 --- a/lib/mains/gt.dart +++ b/lib/mains/gt.dart @@ -4,6 +4,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:riverpod_flutter_tuts/widgets/notifier/counter_widget.dart'; import '../widgets/consumer/random.dart'; +import '../widgets/notifier/random_widget.dart'; import '../widgets/simples/random.dart'; part 'gt.g.dart'; @@ -45,6 +46,7 @@ class MyApp extends ConsumerWidget { const Expanded(child: RandomWidget()), const Expanded(child: Random2Widget()), const Expanded(child: CounterWidget()), + const Expanded(child: Random3Widget()), ], ) diff --git a/lib/outerapi/auth/call/getRandomData.dart b/lib/outerapi/auth/call/getRandomData.dart index dd2c96a..9930e6c 100644 --- a/lib/outerapi/auth/call/getRandomData.dart +++ b/lib/outerapi/auth/call/getRandomData.dart @@ -14,7 +14,7 @@ part 'getRandomData.g.dart'; @riverpod Future 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'); + var ur = Uri.http('127.0.0.1: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; diff --git a/lib/widgets/notifier/counter_widget.dart b/lib/widgets/notifier/counter_widget.dart index ee279f8..77c48f9 100644 --- a/lib/widgets/notifier/counter_widget.dart +++ b/lib/widgets/notifier/counter_widget.dart @@ -11,11 +11,11 @@ class CounterWidget extends ConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { // TODO: implement build - var myRandom = ref.watch(counterNotifierProvider); + var counter = ref.watch(counterNotifierProvider); return Column( children: [ Center( - child: switch (myRandom) { + child: switch (counter) { AsyncData(:final value ) => Text( 'data: $value' ), diff --git a/lib/widgets/notifier/random.dart b/lib/widgets/notifier/random.dart new file mode 100644 index 0000000..e39998d --- /dev/null +++ b/lib/widgets/notifier/random.dart @@ -0,0 +1,29 @@ +import 'dart:convert'; + +import 'package:riverpod_annotation/riverpod_annotation.dart'; +import 'package:riverpod_flutter_tuts/outerapi/auth/model/my_random.dart'; +import 'package:http/http.dart' as http; +part 'random.g.dart'; + +Future fetchData() async { + var ur = Uri.http('127.0.0.1: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; + // Finally, we convert the Map into an Activity instance. + return MyRandom.fromJson(json); +} + +@riverpod +class randomNotif extends _$randomNotif { + @override + Future build() async{ + return fetchData(); + } + + Future again() async { + state = AsyncLoading(); + // ref.invalidateSelf(); + ref.invalidateSelf(); + } +} diff --git a/lib/widgets/notifier/random_widget.dart b/lib/widgets/notifier/random_widget.dart new file mode 100644 index 0000000..5afefd3 --- /dev/null +++ b/lib/widgets/notifier/random_widget.dart @@ -0,0 +1,36 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_flutter_tuts/widgets/notifier/counter.dart'; +import 'package:riverpod_flutter_tuts/widgets/notifier/random.dart'; + + +class Random3Widget extends ConsumerWidget { + const Random3Widget({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + // TODO: implement build + + var myRandom = ref.watch(randomNotifProvider); + return Row( + children: [ + Center( + child: switch (myRandom) { + AsyncData(:final value ) => Text( + 'data: $value' + ), + AsyncError() => const Text('Oops, something unexpected happened'), + _ => const CircularProgressIndicator(), + }), + !myRandom.isLoading ? ElevatedButton(onPressed:() { + if (!myRandom.isLoading) + ref.read(randomNotifProvider.notifier).again(); + }, child: Text(myRandom.isLoading ? "wait":"new")) + : + const Text("loading") + , + ] + ); + } +} \ No newline at end of file