-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture_provider_example.dart
163 lines (147 loc) · 4.08 KB
/
future_provider_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
class Dog with ChangeNotifier {
final String name;
final String breed;
int age;
Dog({
required this.name,
required this.breed,
this.age = 1,
});
void grow() {
age++;
notifyListeners();
}
}
class Babies {
final int age;
Babies({
required this.age,
});
Future<int> getBabies() async {
await Future.delayed(Duration(seconds: 3));
if (age > 1 && age < 5) {
return 4;
} else if (age <= 1) {
return 0;
} else {
return 2;
}
}
}
void main() {
runApp(const FutureProviderExample());
}
// The `FutureProvider` widget is used when the provider needs time to fetch data, so it requires an initial value for the required parameter.
// Widgets that reference FutureProvider are built twice: once for the initial value and once for the return value.
// in most cases, the `FutureBuilder` widget is used instead.
// If a widget needs multiple providers, it requires multiple levels of parents. However, `MultiProvider` makes it easy to read and is essentially syntax sugar.
// In this case, the FureProvider can access the DogChangeNotifierProvider because they are parent and child. Although they may appear to be siblings due to the syntax sugar provided by MultiProvider.
// The type of provider corresponds to the return type of the future method.
class FutureProviderExample extends StatelessWidget {
const FutureProviderExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Dog>(
create: (context) => Dog(name: 'dog06', breed: 'breed06', age: 3),
),
FutureProvider<int>(
initialData: 0,
create: (context) {
final int dogAge = context.read<Dog>().age;
final babies = Babies(age: dogAge);
return babies.getBabies();
},
),
],
child: MaterialApp(
title: 'Provider 06',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Provider 05'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'- name: ${context.watch<Dog>().name}',
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 10.0),
BreedAndAge(),
],
),
),
);
}
}
class BreedAndAge extends StatelessWidget {
const BreedAndAge({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(
'- breed: ${context.select<Dog, String>((Dog dog) => dog.breed)}',
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 10.0),
Age(),
],
);
}
}
class Age extends StatelessWidget {
const Age({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(
'- age: ${context.select<Dog, int>((Dog dog) => dog.age)}',
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 10.0),
Text(
'- number of babies: ${context.watch<int>()}',
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 20.0),
ElevatedButton(
onPressed: () => context.read<Dog>().grow(),
child: Text(
'Grow',
style: TextStyle(fontSize: 20.0),
),
),
],
);
}
}