Skip to content

Commit aae968c

Browse files
authored
Improve StreamBuilder documentation (flutter#23713)
1 parent 06e4ccc commit aae968c

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ import 'framework.dart';
3636
/// termination by overriding [afterDone]. Finally, the summary may be updated
3737
/// on change of stream by overriding [afterDisconnected] and [afterConnected].
3838
///
39-
/// [T] is the type of stream events.
39+
/// `T` is the type of stream events.
4040
///
41-
/// [S] is the type of interaction summary.
41+
/// `S` is the type of interaction summary.
4242
///
4343
/// See also:
4444
///
45-
/// * [StreamBuilder], which is specialized to the case where only the most
46-
/// recent interaction is needed for widget building.
45+
/// * [StreamBuilder], which is specialized for the case where only the most
46+
/// recent interaction is needed for widget building.
4747
abstract class StreamBuilderBase<T, S> extends StatefulWidget {
4848
/// Creates a [StreamBuilderBase] connected to the specified [stream].
4949
const StreamBuilderBase({ Key key, this.stream }) : super(key: key);
@@ -286,9 +286,9 @@ class AsyncSnapshot<T> {
286286
/// See also:
287287
///
288288
/// * [StreamBuilder], which delegates to an [AsyncWidgetBuilder] to build
289-
/// itself based on a snapshot from interacting with a [Stream].
289+
/// itself based on a snapshot from interacting with a [Stream].
290290
/// * [FutureBuilder], which delegates to an [AsyncWidgetBuilder] to build
291-
/// itself based on a snapshot from interacting with a [Future].
291+
/// itself based on a snapshot from interacting with a [Future].
292292
typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnapshot<T> snapshot);
293293

294294
/// Widget that builds itself based on the latest snapshot of interaction with
@@ -333,14 +333,9 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps
333333
/// state is `ConnectionState.active`.
334334
///
335335
/// The initial snapshot data can be controlled by specifying [initialData].
336-
/// You would use this facility to ensure that if the [builder] is invoked
337-
/// before the first event arrives on the stream, the snapshot carries data of
338-
/// your choice rather than the default null value.
339-
///
340-
/// See also:
341-
///
342-
/// * [StreamBuilderBase], which supports widget building based on a computation
343-
/// that spans all interactions made with the stream.
336+
/// This should be used to ensure that the first frame has the expected value,
337+
/// as the builder will always be called before the stream listener has a chance
338+
/// to be processed.
344339
///
345340
/// {@tool sample}
346341
///
@@ -365,25 +360,41 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps
365360
/// )
366361
/// ```
367362
/// {@end-tool}
368-
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1141 is fixed
363+
///
364+
/// See also:
365+
///
366+
/// * [ValueListenableBuilder], which wraps a [ValueListenable] instead of a
367+
/// [Stream].
368+
/// * [StreamBuilderBase], which supports widget building based on a computation
369+
/// that spans all interactions made with the stream.
370+
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1139 is fixed
369371
class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> {
370372
/// Creates a new [StreamBuilder] that builds itself based on the latest
371373
/// snapshot of interaction with the specified [stream] and whose build
372-
/// strategy is given by [builder]. The [initialData] is used to create the
373-
/// initial snapshot. It is null by default.
374+
/// strategy is given by [builder].
375+
///
376+
/// The [initialData] is used to create the initial snapshot.
377+
///
378+
/// The [builder] must not be null.
374379
const StreamBuilder({
375380
Key key,
376381
this.initialData,
377382
Stream<T> stream,
378383
@required this.builder
379-
})
380-
: assert(builder != null),
381-
super(key: key, stream: stream);
384+
}) : assert(builder != null),
385+
super(key: key, stream: stream);
382386

383-
/// The build strategy currently used by this builder. Cannot be null.
387+
/// The build strategy currently used by this builder.
384388
final AsyncWidgetBuilder<T> builder;
385389

386-
/// The data that will be used to create the initial snapshot. Null by default.
390+
/// The data that will be used to create the initial snapshot.
391+
///
392+
/// Providing this value (presumably obtained sychronously somehow when the
393+
/// [Stream] was created) ensures that the first frame will show useful data.
394+
/// Otherwise, the first frame will be built with the value null, regardless
395+
/// of whether a value is available on the stream: since streams are
396+
/// asynchronous, no events from the stream can be obtained before the initial
397+
/// build.
387398
final T initialData;
388399

389400
@override

0 commit comments

Comments
 (0)