In this video, I will explain how to use RxDart and it’s operators. We will also learn about StreamBuilder, StreamController, and StreamTransformer in this video.
RxDart adds additional capabilities to Dart Streams and StreamControllers.
Dart comes with a very decent Streams API out-of-the-box; 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 a number of additional Stream classes, operators (extension methods on the Stream class), and Subjects.
If you are familiar with Observables from other languages, please see the Rx Observables vs Dart Streams comparison chart for notable distinctions between the two.
RxDart 0.23.x moves away from the Observable class, utilizing Dart 2.6’s new extension methods instead. This requires several small refactors that can be easily automated – which is just what we’ve done!
Please follow the instructions on the rxdart_codemod package to automatically upgrade your code to support RxDart 0.23.x.
import 'package:rxdart/rxdart.dart';
void main() {
const konamiKeyCodes = const <int>[
KeyCode.UP,
KeyCode.UP,
KeyCode.DOWN,
KeyCode.DOWN,
KeyCode.LEFT,
KeyCode.RIGHT,
KeyCode.LEFT,
KeyCode.RIGHT,
KeyCode.B,
KeyCode.A,
];
final result = querySelector('#result');
document.onKeyUp
.map((event) => event.keyCode)
.bufferCount(10, 1) // An extension method provided by rxdart
.where((lastTenKeyCodes) => const IterableEquality<int>().equals(lastTenKeyCodes, konamiKeyCodes))
.listen((_) => result.innerHtml = 'KONAMI!');
}
RxDart adds functionality to Dart Streams in three ways:
The Stream class provides different ways to create a Stream: Stream.fromIterable
or Stream.periodic
, for example. RxDart provides additional Stream classes for a variety of tasks, such as combining or merging Streams together!
You can construct the Streams provided by RxDart in two ways. The following examples are equivalent in terms of functionality:
final mergedStream = MergeStream([myFirstStream, mySecondStream]);
Rx
class, which are useful for discovering which types of Streams are provided by RxDart. Under the hood, these factories simply call the the corresponding Stream constructor.
final mergedStream = Rx.merge([myFirstStream, mySecondStream]);
The extension methods provided by RxDart can be used on any Stream
. They convert a source Stream into a new Stream with additional capabilities, such as buffering or throttling events.
Stream.fromIterable([1, 2, 3])
.throttleTime(Duration(seconds: 1))
.listen(print); // prints 3
Dart provides the StreamController class to create and manage a Stream. RxDart offers two additional StreamControllers with additional capabilities, known as Subjects:
In many situations, Streams and Observables work the same way. However, if you’re used to standard Rx Observables, some features of the Stream api may surprise you. We’ve included a table below to help folks understand the differences.
Additional information about the following situations can be found by reading the Rx class documentation.
Web and command-line examples can be found in the example
folder.
In order to run the web examples, please follow these steps:
pub get
pub run build_runner serve example
In order to run the command line example, please follow these steps:
pub get
dart example/example.dart 10
In order to run the flutter example, you must have Flutter installed. For installation instructions, view the online documentation.
cd
into the example/flutter/github_search
directoryflutter doctor
to ensure you have all Flutter dependencies working.flutter packages get
flutter run
Refer to the Changelog to get all release notes.
#flutter #dart #mobileapps #webdev