Subjects

Learn about different stream subjects offered by the RxDart library to manage asynchronous data streams.

Subjects are StreamControllers but with additional features. RxDart provides us with two Subjects: BehaviorSubject and ReplaySubject. The main purpose of these subjects is to give context to new subscribers about the events they missed when they were not subscribed.

BehaviorSubject

The BehaviorSubject captures the latest item that has been added to the controller and emits that as the first item to any new listener.

Press + to interact
final subject = BehaviorSubject<int>();
print("Old subscriber:");
subject.stream.listen(print); // prints 1, 2, 3
subject.add(1);
subject.add(2);
subject.add(3);
await Future.delayed(Duration(milliseconds: 100));
print("New subscribers:");
subject.stream.listen(print); // prints 3
subject.stream.listen(print); // prints 3
  • Line 4: We ...

Access this course and 1400+ top-rated courses and projects.