Add new ConsuemerWidget with same business of RandomWidget.
Add widget to homepage. Two widgets shows same (random) data, and just one request recorded on API server. It's good.
This commit is contained in:
parent
d44fc9a75c
commit
0676de8316
@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../widgets/consumer/random.dart';
|
||||
import '../widgets/simples/random.dart';
|
||||
|
||||
part 'gt.g.dart';
|
||||
@ -40,7 +41,8 @@ class MyApp extends ConsumerWidget {
|
||||
children: <Widget>[
|
||||
const Expanded(child: Text('1')),
|
||||
Expanded(child: Text(value)),
|
||||
const Expanded(child: RandomWidget())
|
||||
const Expanded(child: RandomWidget()),
|
||||
const Expanded(child: Random2Widget())
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
26
lib/widgets/consumer/random.dart
Normal file
26
lib/widgets/consumer/random.dart
Normal file
@ -0,0 +1,26 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_flutter_tuts/outerapi/auth/call/getRandomData.dart';
|
||||
import 'package:riverpod_flutter_tuts/outerapi/auth/model/my_random.dart';
|
||||
|
||||
class Random2Widget extends ConsumerWidget {
|
||||
const Random2Widget({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// TODO: implement build
|
||||
|
||||
final AsyncValue<MyRandom> myRandom = ref.watch(getMyRandomProvider);
|
||||
return Center(
|
||||
child: switch (myRandom) {
|
||||
AsyncData(:final MyRandom value ) => Text(
|
||||
'myRandom: $value'
|
||||
),
|
||||
AsyncError() => const Text('Oops, something unexpected happened'),
|
||||
_ => const CircularProgressIndicator(),
|
||||
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user