What you are going to learn: What is Stream? How to create a Stream? How to use Stream in Dart code? How to use Stream in Flutter UI?
To read the latest version of this article click here: https://mibcoder.com
What is Stream?
Stream is just like Future, here are some key differences that may help you to understand the definition of Stream:
1- The main difference is that Future provides a single result(value/error) on completion but a stream can provide more the one result (values/errors) its just like Iterator which is used in sync coding design pattern but in async coding design pattern instead of Iterator use Stream.
2- In Future get result by implementing “then/catchError/whenComplete”, but Stream only need to implement “listen” for all “value/onError/onDone”.
3- In Future we only get same value which is sended, but Stream contains a number of helper method, by using them we can do number of operation(just like method on an Iterable) once data is in streams before reaching to litner, e.g. i can use “map” operation to change data type int to string.
Definition: A stream is a sequence of asynchronous events. It is like an asynchronous Iterable — where, instead of getting the next event when you ask for it, the stream tells you that there is an event when it is ready.
Note: If function marked with “async” it will return Future and if function marked with “async*” it will return Stream.
How to create Stream?
We can create stream two ways, 1- By using “Asynchronous Generators”. 2- By using “StreamController”.
1- Create Streams Using “Asynchronous Generators”:
To create stream using async Generator, there are two main things:
i- Mark function “async*”(by marking with “async*” defines the function is Async and also defines it will returns Streams).
Stream<int> createStreamUsingAsyncGenerator(Duration interval, [int maxCount]) async* {........}
ii- To emit events on the stream by using “yield”(it will not return(end) function, it only emit the event(value) on stream) and if function is recursive use “yield*”.
yield counter;
Note: When function returns or end, stream will closed.
Remember: Function marked with “async*” will not execute by just calling the function, function body only execute when someone start listen to its stream.
Here is complete example:
2- Create Streams Using “StreamController”:
Two things required to create stream using StreamController:
i- To create function with reruns stream, create object of StreamController and return its “streamController.stream” property
StreamController<int> controller = StreamController<int>(
onListen: startTimer,
onPause: stopTimer,
onResume: startTimer,
onCancel: stopTimer);
return controller.stream;
ii- To Emit events on stream use “streamController.add(…..)” to emit event on stream.
controller.add(counter);
Note: To close stream at any point use “streamController.close()” method of StreamController.
Here is complete example:
How to use Stream in Dart code?
We can use streams in dart by just listen
stream.listen((int value) {
print('Value Form StreamController: $value');
});
Here is its complete example:
How to use Stream in Flutter UI?
Same as FutureBuilder is for Future, for streams there is StreamBuilder widget use to render based on current state of Stream.
StreamBuilder takes two parameters :
1- stream: put method name which is providing stream
2- builder: need to implement method which returning Widget to show based on state of Stream.