1
import 'package:flutter/foundation.dart';
2
import 'package:flutter/widgets.dart';
3
import 'package:provider/provider.dart';
4

5
import 'provider.dart';
6

7
typedef ProviderBuilder<R> = Widget Function(
8
  BuildContext context,
9
  R value,
10
  Widget child,
11
);
12

13
typedef ProxyProviderBuilder<T, R> = R Function(
14
  BuildContext context,
15
  T value,
16
  R previous,
17
);
18

19
typedef ProxyProviderBuilder2<T, T2, R> = R Function(
20
  BuildContext context,
21
  T value,
22
  T2 value2,
23
  R previous,
24
);
25

26
typedef ProxyProviderBuilder3<T, T2, T3, R> = R Function(
27
  BuildContext context,
28
  T value,
29
  T2 value2,
30
  T3 value3,
31
  R previous,
32
);
33

34
typedef ProxyProviderBuilder4<T, T2, T3, T4, R> = R Function(
35
  BuildContext context,
36
  T value,
37
  T2 value2,
38
  T3 value3,
39
  T4 value4,
40
  R previous,
41
);
42

43
typedef ProxyProviderBuilder5<T, T2, T3, T4, T5, R> = R Function(
44
  BuildContext context,
45
  T value,
46
  T2 value2,
47
  T3 value3,
48
  T4 value4,
49
  T5 value5,
50
  R previous,
51
);
52

53
typedef ProxyProviderBuilder6<T, T2, T3, T4, T5, T6, R> = R Function(
54
  BuildContext context,
55
  T value,
56
  T2 value2,
57
  T3 value3,
58
  T4 value4,
59
  T5 value5,
60
  T6 value6,
61
  R previous,
62
);
63

64
/// {@macro provider.proxyprovider}
65
class ProxyProvider0<R> extends InheritedProvider<R> {
66
  /// Initializes [key] for subclasses.
67 3
  ProxyProvider0({
68
    Key key,
69
    Create<R> create,
70
    @required R Function(BuildContext context, R value) update,
71
    UpdateShouldNotify<R> updateShouldNotify,
72
    Dispose<R> dispose,
73
    bool lazy,
74
    TransitionBuilder builder,
75
    Widget child,
76 3
  })  : assert(update != null),
77 3
        super(
78
          key: key,
79
          lazy: lazy,
80
          builder: builder,
81
          create: create,
82
          update: update,
83
          dispose: dispose,
84
          updateShouldNotify: updateShouldNotify,
85
          debugCheckInvalidValueType: kReleaseMode
86
              ? null
87 3
              : (R value) =>
88 3
                  Provider.debugCheckInvalidValueType?.call<R>(value),
89
          child: child,
90
        );
91
}
92

93
/// {@template provider.proxyprovider}
94
/// A provider that builds a value based on other providers.
95
///
96
/// The exposed value is built through either `create` or `update`, then passed
97
/// to [InheritedProvider].
98
///
99
/// As opposed to `create`, `update` may be called more than once.
100
/// It will be called once the first time the value is obtained, then once
101
/// whenever [ProxyProvider] rebuilds or when one of the providers it depends on
102
/// updates.
103
///
104
/// [ProxyProvider] comes in different variants such as [ProxyProvider2]. This
105
/// is syntax sugar on the top of [ProxyProvider0].
106
///
107
/// As such, `ProxyProvider<A, Result>` is equal to:
108
/// ```dart
109
/// ProxyProvider0<Result>(
110
///   update: (context, result) {
111
///     final a = Provider.of<A>(context);
112
///     return update(context, a, result);
113
///   }
114
/// );
115
/// ```
116
///
117
/// Whereas `ProxyProvider2<A, B, Result>` is equal to:
118
/// ```dart
119
/// ProxyProvider0<Result>(
120
///   update: (context, result) {
121
///     final a = Provider.of<A>(context);
122
///     final b = Provider.of<B>(context);
123
///     return update(context, a, b, result);
124
///   }
125
/// );
126
/// ```
127
///
128
/// This last parameter of `update` is the last value returned by either
129
/// `create` or `update`.
130
/// It is `null` by default.
131
///
132
/// `update` must not be `null`.
133
///
134
/// See also:
135
///
136
///  * [Provider], which matches the behavior of [ProxyProvider] but has only
137
///     a `create` callback.
138
/// {@endtemplate}
139
class ProxyProvider<T, R> extends ProxyProvider0<R> {
140
  /// Initializes [key] for subclasses.
141 3
  ProxyProvider({
142
    Key key,
143
    Create<R> create,
144
    @required ProxyProviderBuilder<T, R> update,
145
    UpdateShouldNotify<R> updateShouldNotify,
146
    Dispose<R> dispose,
147
    bool lazy,
148
    TransitionBuilder builder,
149
    Widget child,
150 3
  })  : assert(update != null),
151 3
        super(
152
          key: key,
153
          lazy: lazy,
154
          builder: builder,
155
          create: create,
156 3
          update: (context, value) => update(
157
            context,
158 3
            Provider.of(context),
159
            value,
160
          ),
161
          updateShouldNotify: updateShouldNotify,
162
          dispose: dispose,
163
          child: child,
164
        );
165
}
166

167
/// {@macro provider.proxyprovider}
168
class ProxyProvider2<T, T2, R> extends ProxyProvider0<R> {
169
  /// Initializes [key] for subclasses.
170 3
  ProxyProvider2({
171
    Key key,
172
    Create<R> create,
173
    @required ProxyProviderBuilder2<T, T2, R> update,
174
    UpdateShouldNotify<R> updateShouldNotify,
175
    Dispose<R> dispose,
176
    bool lazy,
177
    TransitionBuilder builder,
178
    Widget child,
179 3
  })  : assert(update != null),
180 3
        super(
181
          key: key,
182
          lazy: lazy,
183
          builder: builder,
184
          create: create,
185 3
          update: (context, value) => update(
186
            context,
187 3
            Provider.of(context),
188 3
            Provider.of(context),
189
            value,
190
          ),
191
          updateShouldNotify: updateShouldNotify,
192
          dispose: dispose,
193
          child: child,
194
        );
195
}
196

197
/// {@macro provider.proxyprovider}
198
class ProxyProvider3<T, T2, T3, R> extends ProxyProvider0<R> {
199
  /// Initializes [key] for subclasses.
200 3
  ProxyProvider3({
201
    Key key,
202
    Create<R> create,
203
    @required ProxyProviderBuilder3<T, T2, T3, R> update,
204
    UpdateShouldNotify<R> updateShouldNotify,
205
    Dispose<R> dispose,
206
    bool lazy,
207
    TransitionBuilder builder,
208
    Widget child,
209 3
  })  : assert(update != null),
210 3
        super(
211
          key: key,
212
          lazy: lazy,
213
          builder: builder,
214
          create: create,
215 3
          update: (context, value) => update(
216
            context,
217 3
            Provider.of(context),
218 3
            Provider.of(context),
219 3
            Provider.of(context),
220
            value,
221
          ),
222
          updateShouldNotify: updateShouldNotify,
223
          dispose: dispose,
224
          child: child,
225
        );
226
}
227

228
/// {@macro provider.proxyprovider}
229
class ProxyProvider4<T, T2, T3, T4, R> extends ProxyProvider0<R> {
230
  /// Initializes [key] for subclasses.
231 3
  ProxyProvider4({
232
    Key key,
233
    Create<R> create,
234
    @required ProxyProviderBuilder4<T, T2, T3, T4, R> update,
235
    UpdateShouldNotify<R> updateShouldNotify,
236
    Dispose<R> dispose,
237
    bool lazy,
238
    TransitionBuilder builder,
239
    Widget child,
240 3
  })  : assert(update != null),
241 3
        super(
242
          key: key,
243
          lazy: lazy,
244
          builder: builder,
245
          create: create,
246 3
          update: (context, value) => update(
247
            context,
248 3
            Provider.of(context),
249 3
            Provider.of(context),
250 3
            Provider.of(context),
251 3
            Provider.of(context),
252
            value,
253
          ),
254
          updateShouldNotify: updateShouldNotify,
255
          dispose: dispose,
256
          child: child,
257
        );
258
}
259

260
/// {@macro provider.proxyprovider}
261
class ProxyProvider5<T, T2, T3, T4, T5, R> extends ProxyProvider0<R> {
262
  /// Initializes [key] for subclasses.
263 3
  ProxyProvider5({
264
    Key key,
265
    Create<R> create,
266
    @required ProxyProviderBuilder5<T, T2, T3, T4, T5, R> update,
267
    UpdateShouldNotify<R> updateShouldNotify,
268
    Dispose<R> dispose,
269
    bool lazy,
270
    TransitionBuilder builder,
271
    Widget child,
272 3
  })  : assert(update != null),
273 3
        super(
274
          key: key,
275
          lazy: lazy,
276
          builder: builder,
277
          create: create,
278 3
          update: (context, value) => update(
279
            context,
280 3
            Provider.of(context),
281 3
            Provider.of(context),
282 3
            Provider.of(context),
283 3
            Provider.of(context),
284 3
            Provider.of(context),
285
            value,
286
          ),
287
          updateShouldNotify: updateShouldNotify,
288
          dispose: dispose,
289
          child: child,
290
        );
291
}
292

293
/// {@macro provider.proxyprovider}
294
class ProxyProvider6<T, T2, T3, T4, T5, T6, R> extends ProxyProvider0<R> {
295
  /// Initializes [key] for subclasses.
296 3
  ProxyProvider6({
297
    Key key,
298
    Create<R> create,
299
    @required ProxyProviderBuilder6<T, T2, T3, T4, T5, T6, R> update,
300
    UpdateShouldNotify<R> updateShouldNotify,
301
    Dispose<R> dispose,
302
    bool lazy,
303
    TransitionBuilder builder,
304
    Widget child,
305 3
  })  : assert(update != null),
306 3
        super(
307
          key: key,
308
          lazy: lazy,
309
          builder: builder,
310
          create: create,
311 3
          update: (context, value) => update(
312
            context,
313 3
            Provider.of(context),
314 3
            Provider.of(context),
315 3
            Provider.of(context),
316 3
            Provider.of(context),
317 3
            Provider.of(context),
318 3
            Provider.of(context),
319
            value,
320
          ),
321
          updateShouldNotify: updateShouldNotify,
322
          dispose: dispose,
323
          child: child,
324
        );
325
}

Read our documentation on viewing source code .

Loading