From 5bdd80d08543a139c5fde20f643aafe3e866b33d Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Mon, 15 May 2017 15:19:45 +0200 Subject: [PATCH] Update docs/sample code for async widgets (#10029) --- packages/flutter/lib/src/widgets/async.dart | 94 ++++++++++----------- 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/packages/flutter/lib/src/widgets/async.dart b/packages/flutter/lib/src/widgets/async.dart index c063e42019..a7ea6a2e42 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): /// -/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` -/// * `AsyncSnapshot.withData(ConnectionState.active, 0)` -/// * `AsyncSnapshot.withData(ConnectionState.active, 1)` +/// * `new AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `new AsyncSnapshot.withData(ConnectionState.active, 0)` +/// * `new AsyncSnapshot.withData(ConnectionState.active, 1)` /// * ... -/// * `AsyncSnapshot.withData(ConnectionState.active, 9)` -/// * `AsyncSnapshot.withData(ConnectionState.done, 9)` +/// * `new AsyncSnapshot.withData(ConnectionState.active, 9)` +/// * `new 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 /// -/// * `AsyncSnapshot.withData(ConnectionState.none, 5)` -/// * `AsyncSnapshot.withData(ConnectionState.waiting, 5)` +/// * `new AsyncSnapshot.withData(ConnectionState.none, 5)` +/// * `new 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 /// -/// * `AsyncSnapshot.withError(ConnectionState.active, 'some error')` +/// * `new AsyncSnapshot.withError(ConnectionState.active, 'some error')` /// /// The data and error fields of snapshots produced are only changed when the /// state is `ConnectionState.active`. @@ -304,6 +304,29 @@ typedef Widget AsyncWidgetBuilder(BuildContext context, AsyncSnapshot snap /// /// * [StreamBuilderBase], which supports widget building based on a computation /// that spans all interactions made with the stream. +/// +/// ## Sample code +/// +/// This sample shows a [StreamBuilder] configuring a text label to show the +/// latest bid received for a lot in an auction. Assume the `_lot` field is +/// set by a selector elsewhere in the UI. +/// +/// ```dart +/// new StreamBuilder( +/// stream: _lot?.bids, // a Stream or null +/// builder: (BuildContext context, AsyncSnapshot snapshot) { +/// if (snapshot.hasError) +/// return new Text('Error: ${snapshot.error}'); +/// switch (snapshot.connectionState) { +/// case ConnectionState.none: return new Text('Select lot'); +/// case ConnectionState.waiting: return new Text('Awaiting bids...'); +/// case ConnectionState.active: return new Text('\$${snapshot.data}'); +/// case ConnectionState.done: return new Text('\$${snapshot.data} (closed)'); +/// default: throw "Unknown: ${snapshot.connectionState}"; +/// } +/// }, +/// ), +/// ``` class StreamBuilder extends StreamBuilderBase> { /// Creates a new [StreamBuilder] that builds itself based on the latest /// snapshot of interaction with the specified [stream] and whose build @@ -356,14 +379,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: /// -/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` -/// * `AsyncSnapshot.withData(ConnectionState.done, 'some data')` +/// * `new AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `new 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: /// -/// * `AsyncSnapshot.withData(ConnectionState.waiting, null)` -/// * `AsyncSnapshot.withError(ConnectionState.done, 'some error')` +/// * `new AsyncSnapshot.withData(ConnectionState.waiting, null)` +/// * `new 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 +394,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: /// -/// * `AsyncSnapshot.withData(ConnectionState.none, 'some data')` -/// * `AsyncSnapshot.withData(ConnectionState.waiting, 'some data')` +/// * `new AsyncSnapshot.withData(ConnectionState.none, 'some data')` +/// * `new 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. @@ -383,25 +406,14 @@ class StreamBuilder extends StreamBuilderBase> { /// /// ## Sample code /// -/// This sample shows a button used to initiate an asynchronous computation -/// with a string result, and a text label reflecting the progress: +/// This sample shows a [FutureBuilder] configuring a text label to show the +/// state of an asynchronous calculation returning a string. Assume the +/// `_calculation` field is set by pressing a button elsewhere in the UI. /// /// ```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) { +/// new FutureBuilder( +/// future: _calculation, // a Future or null +/// builder: (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...'); @@ -411,24 +423,8 @@ class StreamBuilder extends StreamBuilderBase> { /// 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