Flutter:如何从流中获取所有数据,然后监听同一流的新更新?

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

我如何在某个时刻收听流,以便最初传输流中的所有可用数据,然后在更新到来时也收听更新。

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()

在上面的例子中,someMethod()会先打印流中的所有可用值,然后当有新数据添加到流中时。

我怎样才能做到这一点?

检查 rxdart 个主题,也许他们有您要找的东西。