Why use RxDart and how we can use with BLoC Pattern in Flutter?

Wilton Ribeiro
Apr 16 · 6 min read

Streams and Sinks

Stream of Cats 😁

BLoC Pattern

https://www.didierboelens.com/2018/08/reactive-programming---streams---bloc/

A look at RxDart

Observable<T> class

PublishSubject<T> class

PublishSubject<int> subject = new PublishSubject<int>();

/*this listener below will print every integer added to the subject: 1, 2, 3, ...*/
subject.stream.listen(print);
subject.add(1);
subject.add(2);

/*but this listener below will print only the integer added after his initialization: 3, .../*
subject.stream.listen(print);
subject.add(3);

BehaviorSubject<T> class

BehaviorSubject<int> subject = new BehaviorSubject<int>();subject.stream.listen(print); // prints 1,2,3 subject.add(1);
subject.add(2);
subject.add(3);

subject.stream.listen(print); // prints 3

ReplaySubject<T> class

ReplaySubject<int> subject = new ReplaySubject<int>();

subject.add(1);
subject.add(2);
subject.add(3);

subject.stream.listen(print); // prints 1, 2, 3

Now let’s see it in practice

Now I really need your attention
bloc/CounterBloc.dart
App running

Notes and conclusions:

References:

Flutter Community

Articles and Stories from the Flutter Community

Wilton Ribeiro

Written by

Software Engineering student. In love with Flutter and Golang.

Flutter Community

Articles and Stories from the Flutter Community