Junior — Middle
Can you explain what the StreamBuilder component is in the context of working with data streams?
sobes.tech AI
Answer from AI
StreamBuilder is a widget in Flutter that allows you to build an interface based on data coming from a stream (Stream). It listens to the stream and automatically rebuilds its part of the UI whenever a new event occurs.
Example usage:
StreamBuilder<int>(
stream: myNumberStream, // stream of numbers
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
return Text('Current number: ${snapshot.data}');
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return CircularProgressIndicator();
}
},
)
Thus, StreamBuilder simplifies working with asynchronous data and updating the interface in response to stream changes.