Stream In Flutter

jaimis patel
2 min readNov 23, 2020

What is Stream?

Asynchronous programming in Dart is characterised by the Future and Stream classes.

Streams provide an asynchronous sequence of data.

A Future represents a computation that doesn’t complete immediately. Where a normal function returns the result, an asynchronous function returns a Future, which will eventually contain the result. The future will tell you when the result is ready.

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.

How to create a Stream.

When you create a stream then first create a controller. and put a DataType on your StreamController.

StreamController<DataType> _appStreamController =
StreamController();

DataType like a Number, Strings, Booleans, Lists and Map

Create a stream

Stream get getAppStream => _appStreamController.stream;

There are two Type of streams

  1. Single Subscription: There could be a maximum of one listener to this stream.
  2. Broadcast: There could be the infinite number of the listener to this stream.

Syntax of Single Subscription :

StreamController<int> counterController = StreamController<int>();Stream get counterStream => counterController.stream;

Syntax of Broadcast Stream :

StreamController<double> controller = StreamController<double>.broadcast();Stream get controller => controller.stream;

if you want to handle stream than use StreamSubscription<dynamic>

using this you have cancel, resume and paused stream

RxDart

RxDart extends the capabilities of Dart Streams and StreamControllers.

When you use PublishSubject in stream than you can’t received other place at last value but when you use BehaviorSubject than whenever you listen this stream than got always last value

Example:

class AppStream{ static final AppStream _appStream = AppStream._init(); factory AppStream(){  return _appStream; } AppStream.init();//Normal StreamStreamController<int> _normalStreamController =
StreamController.broadcast();
Stream get getNormalStream => _normalStreamController.stream;addItemInStream(){
_normalStreamController.add('1');
}// PublishSubject StreamStreamController<int> _publishStreamController =
PublishSubject<int>();
Stream get getPublishStream => _publishStreamController.stream;// BehaviorSubject StreamStreamController<int> _behaviourStreamController =
BehaviorSubject<int>();
Stream get getBehaviorStream => _behaviourStreamController.stream;}

Listen to other class

class Listen{

late StreamSubscription<int> _normalPictureListener;
late StreamSubscription<int> _publishPictureListener;
late StreamSubscription<int> _publishPictureListener;
_normalPictureListener = AppStream().getNormalStream.listen((event)
{
print('getNormalStream Event ===> $event');
}); _publishPictureListener = AppStream().getPublishStream.listen((event)
{
print('getPublishStream Event ===> $event');
});_publishPictureListener = AppStream().getBehaviorStream.listen((event) {
print('getBehaviorStream Event ===> $event');
});}

--

--