Dart-Future和Stream Wed, Jul 21, 2021 Future 未来的一次回调 Future位于event queue
事件队列中。
Future表示未来的一次性 的任务回调。
wait方法 类似RxJava中的zip
操作符:
1
2
3
4
5
6
final futures = [
Future . delayed ( Duration ( seconds: 3 )),
Future . delayed ( Duration ( seconds: 5 ))
];
Future . wait ( futures )
. then (( value ) => print ( '拿到值: $ value ' ));
Stream 持续监听 Stream是持续性监听,所以就必须要在不需要的时候手动释放。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final stream = File ( '' ). openRead ();
//订阅
final listener = stream . listen (( event ) {
//启动监听
});
//转换成Future
Future future = listener . asFuture ();
//暂停
listener . pause ();
//暂停恢复
listener . resume ();
//取消
listener . cancel ();
Stream的订阅与广播 订阅 Stream默认就是单订阅模式。如果重复调用listen()
方法就会报错,比如:
1
2
3
4
5
6
final stream = File ( '' ). openRead ();
//订阅
final listener = stream . listen (( event ) {
//启动监听
});
final listener1 = stream . listen (( event ) {});
报错:
广播模式 广播模式允许多订阅。可以把订阅模式转为广播模式:
1
2
3
4
5
6
7
8
final stream = File ( '' ). openRead ();
stream . asBroadcastStream ();
//订阅
final listener = stream . listen (( event ) {
//启动监听
});
final listener1 = stream . listen (( event ) {});
也可以通过StreamController
直接创建广播:
1
2
3
4
5
6
final streamController = StreamController . broadcast ();
streamController . stream . listen (( event ) {
// todo 回调
});
streamController . add ( 'value' );
streamController . close ();
StreamSink