Intro to RxDart

Maya Mohite
Globant
Published in
3 min readFeb 12, 2020

Most of the time in mobile applications, we have to perform multiple async operations in parallel or one after the other and depending on the result we need to transform the data and then update the UI. To handle these complex scenarios Rx comes to rescue.

Reactive comes from the word react, which means reacting to changes in the state. RxDart falls under reactive programming, which is only concerned about data streams and propagation of change.

One question that comes to mind is, why to use RxDart?
Dart comes with decent Stream APIs, rather than attempting to provide an alternative to this API, RxDart adds functionality from the ‘reactive extensions specification’ on top of it. RxDart does not provide its own ‘Observable’ class as a replacement for Dart Streams. Rather, it provides several additional stream classes, operators (extension methods on the stream class), and Subjects.

Why reactive programming?

  1. Easier to chain multiple requests and apply transformations.
  2. Transfer one stream into another stream with multiple capabilities.
  3. Callback processing and tracking errors.
  4. Takes care of multi-threading by doing complex thread operations, keeping everything synchronized and returning the relevant things.

Stream

A stream is a sequence of asynchronous events. It is like a pipe when you put data at one end and if there’s a listener on the other end that listener will receive that value. With StreamController/Subject you can put data on the Stream. Since RxDart 0.23.x Stream is used instead of Observable.

Subjects-
Dart already provides StreamController to create and manage Streams. However, RxDart provides Subject which is the same as StreamControllers but with additional capabilities.

Subject types:
1. BehaviouralSubject
- This is the broadcast stream controller, meaning the Subject stream can listen to multiple times. BehaviouralSubject catches the latest value or error. When a new listener subscribes to the Stream, the latest value or error will be emitted to the listener.

2. PublishSubject- This is a default broadcast stream controller. value/error added after subscription-only will be emitted to the listener.

3. ReplaySubject- This is a special broadcast StreamController that captures all the items that have been added to the controller. This subject allows sending data, error and done events to the listener. As items are added to the Subject, ReplaySubject will store them. When the Stream is listened to, those recorded items will be emitted to the listener. After that, any new added will be delivered to the listeners.

To limit the stored elements we can set maxSize.

Comparison of StreamController and Subjects -

Comparision

As we have seen in previous examples we have to listen to the Stream to capture events. Below is the syntax of Stream Subscription :

onData, onError & onDone methods
onData
: This method is called each time whenever a new event is published. Here we can perform an action on the event.
onError: This is called when an unhandled exception is thrown during the RxDart framework code or our event handling code.
onDone : This is called when a sequence of events associated with a stream is complete.
cancelOnError : The default value of cancelOnError is false. If cancelOnError is true, the subscription is automatically canceled when the first error event is delivered.

So far we have seen the basics of RxDart and Stream.

Now, let’s see how to use all these components with flutter UI.

bloc.dart
ticket_widget.dart

This is all about the Streams with RxDart. In the next blog, we are going to explore Rx operators in detail.

Hope you find this blog helpful. Thanks!!

References:

https://pub.dev/documentation/rxdart/latest/

--

--