@@ -267,13 +267,13 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
267
267
/// a [Stream] .
268
268
///
269
269
/// Widget rebuilding is scheduled by each interaction, using [State.setState] ,
270
- /// but is otherwise decoupled from the timing of the stream. The [build] method
270
+ /// but is otherwise decoupled from the timing of the stream. The [builder]
271
271
/// is called at the discretion of the Flutter pipeline, and will thus receive a
272
272
/// timing-dependent sub-sequence of the snapshots that represent the
273
273
/// interaction with the stream.
274
274
///
275
275
/// As an example, when interacting with a stream producing the integers
276
- /// 0 through 9, the [build] method may be called with any ordered sub-sequence
276
+ /// 0 through 9, the [builder] may be called with any ordered sub-sequence
277
277
/// of the following snapshots that includes the last one (the one with
278
278
/// ConnectionState.done):
279
279
///
@@ -284,8 +284,9 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
284
284
/// * `new AsyncSnapshot<int>.withData(ConnectionState.active, 9)`
285
285
/// * `new AsyncSnapshot<int>.withData(ConnectionState.done, 9)`
286
286
///
287
- /// The actual sequence of invocations of [build] depends on the relative timing
288
- /// of events produced by the stream and the build rate of the Flutter pipeline.
287
+ /// The actual sequence of invocations of the [builder] depends on the relative
288
+ /// timing of events produced by the stream and the build rate of the Flutter
289
+ /// pipeline.
289
290
///
290
291
/// Changing the [StreamBuilder] configuration to another stream during event
291
292
/// generation introduces snapshot pairs of the form
@@ -303,6 +304,11 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
303
304
/// The data and error fields of snapshots produced are only changed when the
304
305
/// state is `ConnectionState.active` .
305
306
///
307
+ /// The initial snapshot data can be controlled by specifying [initialData] .
308
+ /// You would use this facility to ensure that if the [builder] is invoked
309
+ /// before the first event arrives on the stream, the snapshot carries data of
310
+ /// your choice rather than the default null value.
311
+ ///
306
312
/// See also:
307
313
///
308
314
/// * [StreamBuilderBase] , which supports widget building based on a computation
@@ -333,19 +339,25 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
333
339
class StreamBuilder <T > extends StreamBuilderBase <T , AsyncSnapshot <T >> {
334
340
/// Creates a new [StreamBuilder] that builds itself based on the latest
335
341
/// snapshot of interaction with the specified [stream] and whose build
336
- /// strategy is given by [builder] .
342
+ /// strategy is given by [builder] . The [initialData] is used to create the
343
+ /// initial snapshot. It is null by default.
337
344
const StreamBuilder ({
338
345
Key key,
346
+ this .initialData,
339
347
Stream <T > stream,
340
348
@required this .builder
341
- }) : assert (builder != null ),
342
- super (key: key, stream: stream);
349
+ })
350
+ : assert (builder != null ),
351
+ super (key: key, stream: stream);
343
352
344
353
/// The build strategy currently used by this builder. Cannot be null.
345
354
final AsyncWidgetBuilder <T > builder;
346
355
356
+ /// The data that will be used to create the initial snapshot. Null by default.
357
+ final T initialData;
358
+
347
359
@override
348
- AsyncSnapshot <T > initial () => new AsyncSnapshot <T >.nothing (); // ignore: prefer_const_constructors
360
+ AsyncSnapshot <T > initial () => new AsyncSnapshot <T >.withData ( ConnectionState .none, initialData);
349
361
350
362
@override
351
363
AsyncSnapshot <T > afterConnected (AsyncSnapshot <T > current) => current.inState (ConnectionState .waiting);
@@ -391,6 +403,11 @@ class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> {
391
403
/// * `new AsyncSnapshot<String>.withData(ConnectionState.waiting, null)`
392
404
/// * `new AsyncSnapshot<String>.withError(ConnectionState.done, 'some error')`
393
405
///
406
+ /// The initial snapshot data can be controlled by specifying [initialData] . You
407
+ /// would use this facility to ensure that if the [builder] is invoked before
408
+ /// the future completes, the snapshot carries data of your choice rather than
409
+ /// the default null value.
410
+ ///
394
411
/// The data and error fields of the snapshot change only as the connection
395
412
/// state field transitions from `waiting` to `done` , and they will be retained
396
413
/// when changing the [FutureBuilder] configuration to another future. If the
@@ -437,6 +454,7 @@ class FutureBuilder<T> extends StatefulWidget {
437
454
const FutureBuilder ({
438
455
Key key,
439
456
this .future,
457
+ this .initialData,
440
458
@required this .builder
441
459
}) : assert (builder != null ),
442
460
super (key: key);
@@ -448,6 +466,9 @@ class FutureBuilder<T> extends StatefulWidget {
448
466
/// The build strategy currently used by this builder. Cannot be null.
449
467
final AsyncWidgetBuilder <T > builder;
450
468
469
+ /// The data that will be used to create the initial snapshot. Null by default.
470
+ final T initialData;
471
+
451
472
@override
452
473
State <FutureBuilder <T >> createState () => new _FutureBuilderState <T >();
453
474
}
@@ -458,11 +479,12 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
458
479
/// calling setState from stale callbacks, e.g. after disposal of this state,
459
480
/// or after widget reconfiguration to a new Future.
460
481
Object _activeCallbackIdentity;
461
- AsyncSnapshot <T > _snapshot = new AsyncSnapshot < T >. nothing (); // ignore: prefer_const_constructors
482
+ AsyncSnapshot <T > _snapshot;
462
483
463
484
@override
464
485
void initState () {
465
486
super .initState ();
487
+ _snapshot = new AsyncSnapshot <T >.withData (ConnectionState .none, widget.initialData);
466
488
_subscribe ();
467
489
}
468
490
0 commit comments