Unverified Commit aae968cd authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Improve StreamBuilder documentation (#23713)

parent 06e4ccc9
...@@ -36,13 +36,13 @@ import 'framework.dart'; ...@@ -36,13 +36,13 @@ import 'framework.dart';
/// termination by overriding [afterDone]. Finally, the summary may be updated /// termination by overriding [afterDone]. Finally, the summary may be updated
/// on change of stream by overriding [afterDisconnected] and [afterConnected]. /// on change of stream by overriding [afterDisconnected] and [afterConnected].
/// ///
/// [T] is the type of stream events. /// `T` is the type of stream events.
/// ///
/// [S] is the type of interaction summary. /// `S` is the type of interaction summary.
/// ///
/// See also: /// See also:
/// ///
/// * [StreamBuilder], which is specialized to the case where only the most /// * [StreamBuilder], which is specialized for the case where only the most
/// recent interaction is needed for widget building. /// recent interaction is needed for widget building.
abstract class StreamBuilderBase<T, S> extends StatefulWidget { abstract class StreamBuilderBase<T, S> extends StatefulWidget {
/// Creates a [StreamBuilderBase] connected to the specified [stream]. /// Creates a [StreamBuilderBase] connected to the specified [stream].
...@@ -333,14 +333,9 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps ...@@ -333,14 +333,9 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps
/// state is `ConnectionState.active`. /// state is `ConnectionState.active`.
/// ///
/// The initial snapshot data can be controlled by specifying [initialData]. /// The initial snapshot data can be controlled by specifying [initialData].
/// You would use this facility to ensure that if the [builder] is invoked /// This should be used to ensure that the first frame has the expected value,
/// before the first event arrives on the stream, the snapshot carries data of /// as the builder will always be called before the stream listener has a chance
/// your choice rather than the default null value. /// to be processed.
///
/// See also:
///
/// * [StreamBuilderBase], which supports widget building based on a computation
/// that spans all interactions made with the stream.
/// ///
/// {@tool sample} /// {@tool sample}
/// ///
...@@ -365,25 +360,41 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps ...@@ -365,25 +360,41 @@ typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnaps
/// ) /// )
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1141 is fixed ///
/// See also:
///
/// * [ValueListenableBuilder], which wraps a [ValueListenable] instead of a
/// [Stream].
/// * [StreamBuilderBase], which supports widget building based on a computation
/// that spans all interactions made with the stream.
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1139 is fixed
class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> { class StreamBuilder<T> extends StreamBuilderBase<T, AsyncSnapshot<T>> {
/// Creates a new [StreamBuilder] that builds itself based on the latest /// Creates a new [StreamBuilder] that builds itself based on the latest
/// snapshot of interaction with the specified [stream] and whose build /// snapshot of interaction with the specified [stream] and whose build
/// strategy is given by [builder]. The [initialData] is used to create the /// strategy is given by [builder].
/// initial snapshot. It is null by default. ///
/// The [initialData] is used to create the initial snapshot.
///
/// The [builder] must not be null.
const StreamBuilder({ const StreamBuilder({
Key key, Key key,
this.initialData, this.initialData,
Stream<T> stream, Stream<T> stream,
@required this.builder @required this.builder
}) }) : assert(builder != null),
: assert(builder != null),
super(key: key, stream: stream); super(key: key, stream: stream);
/// The build strategy currently used by this builder. Cannot be null. /// The build strategy currently used by this builder.
final AsyncWidgetBuilder<T> builder; final AsyncWidgetBuilder<T> builder;
/// The data that will be used to create the initial snapshot. Null by default. /// The data that will be used to create the initial snapshot.
///
/// Providing this value (presumably obtained sychronously somehow when the
/// [Stream] was created) ensures that the first frame will show useful data.
/// Otherwise, the first frame will be built with the value null, regardless
/// of whether a value is available on the stream: since streams are
/// asynchronous, no events from the stream can be obtained before the initial
/// build.
final T initialData; final T initialData;
@override @override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment