krishnakumarcn
krishnakumarcn

Reputation: 4179

Flutter: How to get all the data from a stream and then listen for the same stream for new updates?

How can I listen to a stream at some point so that initially all the data available in the stream will get delivered and then the updates also getting listen to whenever they are coming.

StreamController<String> controller = StreamController<String>.broadcast();
Stream stream = controller.stream;;
StreamSubscription<String> streamSubscription = stream.listen((value) {
      print('Value from controller: $value');
}); 
controller.sink.add("1"); // prints "Value from controller: 1"
controller.sink.add("2"); // prints "Value from controller: 2"

stream.someMethod() ; // prints
                      // Value from controller: 1
                      // Value from controller: 2
controller.sink.add("3"); // prints "Value from controller: 3" by the someMethod()

In the above example, the someMethod() will first print all the available values in the stream and then when a new data added to the stream.

How can I achieve this?

Upvotes: 1

Views: 9710

Answers (1)

Hassan Saleh
Hassan Saleh

Reputation: 984

Checkout rxdart subjects, maybe they have what you are looking for.

Upvotes: 2

Related Questions