Skip to content

Commit bc390ef

Browse files
authored
Add sample code to FutureBuilder (flutter#9952)
1 parent 9b56c1c commit bc390ef

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

packages/flutter/lib/src/widgets/async.dart

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,28 @@ typedef Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snap
274274
/// of the following snapshots that includes the last one (the one with
275275
/// ConnectionState.done):
276276
///
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)`
280280
/// * ...
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)`
283283
///
284284
/// The actual sequence of invocations of [build] depends on the relative timing
285285
/// of events produced by the stream and the build rate of the Flutter pipeline.
286286
///
287287
/// Changing the [StreamBuilder] configuration to another stream during event
288288
/// generation introduces snapshot pairs of the form
289289
///
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)`
292292
///
293293
/// The latter will be produced only when the new stream is non-null. The former
294294
/// only when the old stream is non-null.
295295
///
296296
/// The stream may produce errors, resulting in snapshots of the form
297297
///
298-
/// * `new AsyncSnapshot<int>(ConnectionState.active, null, 'some error')`
298+
/// * `AsyncSnapshot<int>.withError(ConnectionState.active, 'some error')`
299299
///
300300
/// The data and error fields of snapshots produced are only changed when the
301301
/// state is `ConnectionState.active`.
@@ -356,30 +356,80 @@ class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> {
356356
/// For a future that completes successfully with data, the [builder] may
357357
/// be called with either both or only the latter of the following snapshots:
358358
///
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')`
361361
///
362362
/// For a future completing with an error, the [builder] may be called with
363363
/// either both or only the latter of:
364364
///
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')`
367367
///
368368
/// The data and error fields of the snapshot change only as the connection
369369
/// state field transitions from `waiting` to `done`, and they will be retained
370370
/// when changing the [FutureBuilder] configuration to another future. If the
371371
/// old future has already completed successfully with data as above, changing
372372
/// configuration to a new future results in snapshot pairs of the form:
373373
///
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')`
376376
///
377377
/// In general, the latter will be produced only when the new future is
378378
/// non-null. The former only when the old future is non-null.
379379
///
380380
/// A [FutureBuilder] behaves identically to a [StreamBuilder] configured with
381381
/// `future?.asStream()`, except that snapshots with `ConnectionState.active`
382382
/// 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+
/// ```
383433
class FutureBuilder<T> extends StatefulWidget {
384434
/// Creates a widget that builds itself based on the latest snapshot of
385435
/// interaction with a [Future].

0 commit comments

Comments
 (0)