简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM