From bc390ef4d07e91f4f5b835c43ce50d93188456a6 Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Wed, 10 May 2017 13:42:26 +0200 Subject: [PATCH] Add sample code to FutureBuilder (#9952) --- packages/flutter/lib/src/widgets/async.dart | 78 +++++++++++++++++---- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/widgets/async.dart b/packages/flutter/lib/src/widgets/async.dart index 7c09cdd425..c063e42019 100644 --- a/packages/flutter/lib/src/widgets/async.dart +++ b/packages/flutter/lib/src/widgets/async.dart @@ -274,12 +274,12 @@ typedef Widget AsyncWidgetBuilder(BuildContext context, AsyncSnapshot snap /// of the following snapshots that includes the last one (the one with /// ConnectionState.done): /// -/// * `new AsyncSnapshot(ConnectionState.waiting, null, null)` -/// * `new AsyncSnapshot(ConnectionState.active, 0, null)` -/// * `new AsyncSnapshot(ConnectionState.active, 1, null)` +/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `AsyncSnapshot.withData(ConnectionState.active, 0)` +/// * `AsyncSnapshot.withData(ConnectionState.active, 1)` /// * ... -/// * `new AsyncSnapshot(ConnectionState.active, 9, null)` -/// * `new AsyncSnapshot(ConnectionState.done, 9, null)` +/// * `AsyncSnapshot.withData(ConnectionState.active, 9)` +/// * `AsyncSnapshot.withData(ConnectionState.done, 9)` /// /// The actual sequence of invocations of [build] depends on the relative timing /// of events produced by the stream and the build rate of the Flutter pipeline. @@ -287,15 +287,15 @@ typedef Widget AsyncWidgetBuilder(BuildContext context, AsyncSnapshot snap /// Changing the [StreamBuilder] configuration to another stream during event /// generation introduces snapshot pairs of the form /// -/// * `new AsyncSnapshot(ConnectionState.none, 5, null)` -/// * `new AsyncSnapshot(ConnectionState.waiting, 5, null)` +/// * `AsyncSnapshot.withData(ConnectionState.none, 5)` +/// * `AsyncSnapshot.withData(ConnectionState.waiting, 5)` /// /// The latter will be produced only when the new stream is non-null. The former /// only when the old stream is non-null. /// /// The stream may produce errors, resulting in snapshots of the form /// -/// * `new AsyncSnapshot(ConnectionState.active, null, 'some error')` +/// * `AsyncSnapshot.withError(ConnectionState.active, 'some error')` /// /// The data and error fields of snapshots produced are only changed when the /// state is `ConnectionState.active`. @@ -356,14 +356,14 @@ class StreamBuilder extends StreamBuilderBase> { /// For a future that completes successfully with data, the [builder] may /// be called with either both or only the latter of the following snapshots: /// -/// * `new AsyncSnapshot(ConnectionState.waiting, null, null)` -/// * `new AsyncSnapshot(ConnectionState.done, 'some data', null)` +/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `AsyncSnapshot.withData(ConnectionState.done, 'some data')` /// /// For a future completing with an error, the [builder] may be called with /// either both or only the latter of: /// -/// * `new AsyncSnapshot(ConnectionState.waiting, null, null)` -/// * `new AsyncSnapshot(ConnectionState.done, null, 'some error')` +/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `AsyncSnapshot.withError(ConnectionState.done, 'some error')` /// /// The data and error fields of the snapshot change only as the connection /// state field transitions from `waiting` to `done`, and they will be retained @@ -371,8 +371,8 @@ class StreamBuilder extends StreamBuilderBase> { /// old future has already completed successfully with data as above, changing /// configuration to a new future results in snapshot pairs of the form: /// -/// * `new AsyncSnapshot(ConnectionState.none, 'some data', null)` -/// * `new AsyncSnapshot(ConnectionState.waiting, 'some data', null)` +/// * `AsyncSnapshot.withData(ConnectionState.none, 'some data')` +/// * `AsyncSnapshot.withData(ConnectionState.waiting, 'some data')` /// /// In general, the latter will be produced only when the new future is /// non-null. The former only when the old future is non-null. @@ -380,6 +380,56 @@ class StreamBuilder extends StreamBuilderBase> { /// A [FutureBuilder] behaves identically to a [StreamBuilder] configured with /// `future?.asStream()`, except that snapshots with `ConnectionState.active` /// may appear for the latter, depending on how the stream is implemented. +/// +/// ## Sample code +/// +/// This sample shows a button used to initiate an asynchronous computation +/// with a string result, and a text label reflecting the progress: +/// +/// ```dart +/// class Calculator extends StatefulWidget { +/// @override +/// _CalculatorState createState() => new _CalculatorState(); +/// } +/// +/// class _CalculatorState extends State { +/// Future _calculation; +/// +/// void _initiateCalculation() { +/// setState(() { +/// _calculation = new Future.delayed(const Duration(seconds: 2), () => 'long awaited'); +/// }); +/// } +/// +/// Widget _buildCalculationWidget(BuildContext context, AsyncSnapshot snapshot) { +/// switch (snapshot.connectionState) { +/// case ConnectionState.none: return new Text('Press button to start'); +/// case ConnectionState.waiting: return new Text('Awaiting result...'); +/// default: +/// if (snapshot.hasError) +/// return new Text('Error: ${snapshot.error}'); +/// else +/// return new Text('Result: ${snapshot.data}'); +/// } +/// } +/// +/// @override +/// Widget build(BuildContext context) { +/// return new Scaffold( +/// body: new Center( +/// child: new FutureBuilder( +/// future: _calculation, +/// builder: _buildCalculationWidget, +/// ), +/// ), +/// floatingActionButton: new FloatingActionButton( +/// onPressed: _initiateCalculation, +/// child: new Icon(Icons.forward), +/// ), +/// ); +/// } +/// } +/// ``` class FutureBuilder extends StatefulWidget { /// Creates a widget that builds itself based on the latest snapshot of /// interaction with a [Future].