Flutter - using rxDart BehaviorSubject with Observable for global state management

Viewed 2360

I am just trying the BehaviorSubject example code from here https://fireship.io/lessons/flutter-state-management-guide/

installed rxdart and observable from pub.dev, but this line errs

Observable get stream$ => _counter.stream;

error: A value of type 'ValueStream' can't be returned from function 'stream$' because it has a return type of 'Observable'. (return_of_invalid_type at lib\models\globals.dart:54)

2 Answers

I guess that Observable class is deprecated since Dart 2.7 in which extension methods are introduced, so you have to use the normal Stream class and import RxDart plugin to get the different capabilities that RxDart offers. In case you want to combine two streams or more you can use Rx class. I also encourage you to use flutter_bloc plugin which deals with InheritedWidget, StreamBuilder, and Streams in a great way.

The Fireship.io video is an excellent survey of state management options available in Flutter. Unfortunately, it is slightly out of date.

Observable can now be safely replaced by Stream

Observable get stream$ => _counter.stream;

If you need to combine streams, use Rx.combineLatest2 instead of Observable.combineLatest2 as the latter is now obsolete.

Related