@@ -274,28 +274,28 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
274
274
/// of the following snapshots that includes the last one (the one with
275
275
/// ConnectionState.done):
276
276
///
277
- /// * `new AsyncSnapshot<int>(ConnectionState.waiting, null , null)`
278
- /// * `new AsyncSnapshot<int>(ConnectionState.active, 0, null )`
279
- /// * `new AsyncSnapshot<int>(ConnectionState.active, 1, null )`
277
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.waiting, null)`
278
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.active, 0)`
279
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.active, 1)`
280
280
/// * ...
281
- /// * `new AsyncSnapshot<int>(ConnectionState.active, 9, null )`
282
- /// * `new AsyncSnapshot<int>(ConnectionState.done, 9, null )`
281
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.active, 9)`
282
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.done, 9)`
283
283
///
284
284
/// The actual sequence of invocations of [build] depends on the relative timing
285
285
/// of events produced by the stream and the build rate of the Flutter pipeline.
286
286
///
287
287
/// Changing the [StreamBuilder] configuration to another stream during event
288
288
/// generation introduces snapshot pairs of the form
289
289
///
290
- /// * `new AsyncSnapshot<int>(ConnectionState.none, 5, null )`
291
- /// * `new AsyncSnapshot<int>(ConnectionState.waiting, 5, null )`
290
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.none, 5)`
291
+ /// * `AsyncSnapshot<int>.withData (ConnectionState.waiting, 5)`
292
292
///
293
293
/// The latter will be produced only when the new stream is non-null. The former
294
294
/// only when the old stream is non-null.
295
295
///
296
296
/// The stream may produce errors, resulting in snapshots of the form
297
297
///
298
- /// * `new AsyncSnapshot<int>(ConnectionState.active, null , 'some error')`
298
+ /// * `AsyncSnapshot<int>.withError (ConnectionState.active, 'some error')`
299
299
///
300
300
/// The data and error fields of snapshots produced are only changed when the
301
301
/// state is `ConnectionState.active` .
@@ -356,30 +356,80 @@ class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> {
356
356
/// For a future that completes successfully with data, the [builder] may
357
357
/// be called with either both or only the latter of the following snapshots:
358
358
///
359
- /// * `new AsyncSnapshot<String>(ConnectionState.waiting, null , null)`
360
- /// * `new AsyncSnapshot<String>(ConnectionState.done, 'some data', null )`
359
+ /// * `AsyncSnapshot<String>.withData (ConnectionState.waiting, null)`
360
+ /// * `AsyncSnapshot<String>.withData (ConnectionState.done, 'some data')`
361
361
///
362
362
/// For a future completing with an error, the [builder] may be called with
363
363
/// either both or only the latter of:
364
364
///
365
- /// * `new AsyncSnapshot<String>(ConnectionState.waiting, null , null)`
366
- /// * `new AsyncSnapshot<String>(ConnectionState.done, null , 'some error')`
365
+ /// * `AsyncSnapshot<String>.withData (ConnectionState.waiting, null)`
366
+ /// * `AsyncSnapshot<String>.withError (ConnectionState.done, 'some error')`
367
367
///
368
368
/// The data and error fields of the snapshot change only as the connection
369
369
/// state field transitions from `waiting` to `done` , and they will be retained
370
370
/// when changing the [FutureBuilder] configuration to another future. If the
371
371
/// old future has already completed successfully with data as above, changing
372
372
/// configuration to a new future results in snapshot pairs of the form:
373
373
///
374
- /// * `new AsyncSnapshot<String>(ConnectionState.none, 'some data', null )`
375
- /// * `new AsyncSnapshot<String>(ConnectionState.waiting, 'some data', null )`
374
+ /// * `AsyncSnapshot<String>.withData (ConnectionState.none, 'some data')`
375
+ /// * `AsyncSnapshot<String>.withData (ConnectionState.waiting, 'some data')`
376
376
///
377
377
/// In general, the latter will be produced only when the new future is
378
378
/// non-null. The former only when the old future is non-null.
379
379
///
380
380
/// A [FutureBuilder] behaves identically to a [StreamBuilder] configured with
381
381
/// `future?.asStream()` , except that snapshots with `ConnectionState.active`
382
382
/// may appear for the latter, depending on how the stream is implemented.
383
+ ///
384
+ /// ## Sample code
385
+ ///
386
+ /// This sample shows a button used to initiate an asynchronous computation
387
+ /// with a string result, and a text label reflecting the progress:
388
+ ///
389
+ /// ```dart
390
+ /// class Calculator extends StatefulWidget {
391
+ /// @override
392
+ /// _CalculatorState createState() => new _CalculatorState();
393
+ /// }
394
+ ///
395
+ /// class _CalculatorState extends State<Calculator> {
396
+ /// Future<String> _calculation;
397
+ ///
398
+ /// void _initiateCalculation() {
399
+ /// setState(() {
400
+ /// _calculation = new Future.delayed(const Duration(seconds: 2), () => 'long awaited');
401
+ /// });
402
+ /// }
403
+ ///
404
+ /// Widget _buildCalculationWidget(BuildContext context, AsyncSnapshot<String> snapshot) {
405
+ /// switch (snapshot.connectionState) {
406
+ /// case ConnectionState.none: return new Text('Press button to start');
407
+ /// case ConnectionState.waiting: return new Text('Awaiting result...');
408
+ /// default:
409
+ /// if (snapshot.hasError)
410
+ /// return new Text('Error: ${snapshot.error}');
411
+ /// else
412
+ /// return new Text('Result: ${snapshot.data}');
413
+ /// }
414
+ /// }
415
+ ///
416
+ /// @override
417
+ /// Widget build(BuildContext context) {
418
+ /// return new Scaffold(
419
+ /// body: new Center(
420
+ /// child: new FutureBuilder(
421
+ /// future: _calculation,
422
+ /// builder: _buildCalculationWidget,
423
+ /// ),
424
+ /// ),
425
+ /// floatingActionButton: new FloatingActionButton(
426
+ /// onPressed: _initiateCalculation,
427
+ /// child: new Icon(Icons.forward),
428
+ /// ),
429
+ /// );
430
+ /// }
431
+ /// }
432
+ /// ```
383
433
class FutureBuilder <T > extends StatefulWidget {
384
434
/// Creates a widget that builds itself based on the latest snapshot of
385
435
/// interaction with a [Future] .
0 commit comments