The first step when using StreamBuilder is setting its stream property, and we did that with the following command:
StreamBuilder(
stream: numberStream,
With the initialdata property, you can specify which data to show when the screen loads and before the first event is emitted.
initialData: 0,
Then you write a builder. This is a function that takes the current context and a snapshot, which contains the data emitted by the stream, in the data property. Hence, this is triggered automatically each time Stream emits a new event, and new data is available. In our example, we check whether the snapshot contains some data with the help of the following command:
if (snapshot.hasData) {...
If there is data in the snapshot, we show it in a Text:
return Center(
child: Text(
snapshot.data.toString(),
style: TextStyle(fontSize: 96),
));
The snapshot hasError property allows you to check whether errors were returned. As usual, this is extremely...