我怎样才能在某个时候收听流,以便最初流中可用的所有数据都将被传递,然后更新也会在它们到来时被收听。
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()
将首先打印流中的所有可用值,然后当有新数据添加到流中时。
我怎样才能做到这一点?