Flutter Bloc 02 - 基础对象 Stream 流操作

189 阅读1分钟

本节目标

  • Stream 创建
  • StreamController 控制
  • StreamSubscription 订阅
  • StreamTransformer 转换
  • Sink、StreamSink、EventSink 修改数据

视频

www.bilibili.com/video/BV1YK…

代码

github.com/ducafecat/f…

正文

核心类

名称说明
Stream事件流或者管道
StreamController事件管理者
StreamSubscription管理事件订阅,如 cacenl、pause
StreamSink流 Sink 入口,提供如 add、addStream 等
EventSink事件 Sink 入口
StreamTransformer流转换

准备函数

// 打印流列表
printStream(Stream<Object> stream) async {
  await for (var val in stream) {
    print(val);
  }
}

// 异步函数i
Future<int> funi = Future(() {
  return 100;
});

// 异步函数ii
Future<int> funii = Future(() {
  return 200;
});

stream 创建

延迟间隔

periodic() async {
  Stream<int> stream = Stream<int>.periodic(Duration(seconds: 1), (val) => val);
  await printStream(stream);
}

futrue 数据源

fromFuture() async {
  Stream<int> stream = Stream<int>.fromFuture(funi);
  await printStream(stream);
}

futrues 多数据源

fromFutures() async {
  Stream<int> stream = Stream<int>.fromFutures([
    funi,
    funii,
  ]);
  await printStream(stream);
}

stream 监听

单对单

listen() async {
  Stream<int> stream = Stream<int>.periodic(Duration(seconds: 1), (val) => val);
  stream.listen(
    (event) {
      print(event);
    },
    onError: (err) {
      print(err);
    },
    onDone: () {},
    cancelOnError: true,
  );
}

广播

boardcast() async {
  Stream<int> stream = Stream<int>.periodic(Duration(seconds: 1), (val) => val)
      .asBroadcastStream();
  stream.listen((event) {
    print(event);
  });
  stream.listen((event) {
    print(event);
  });
}

操作 task skip

opt() async {
  Stream<int> stream = Stream<int>.fromIterable([1, 2, 3, 4, 5]);
  stream = stream.take(3);
  // stream = stream.skip(2);

  stream.listen((event) {
    print(event);
  });
}

StreamController 流控制类

单点

scListen() async {
  StreamController sc = StreamController(
      onListen: () => print("onListen"),
      onPause: () => print("onPause"),
      onResume: () => print("onResume"),
      onCancel: () => print("onCancel"),
      sync: false);

  // 订阅对象
  StreamSubscription ss = sc.stream.listen(((event) {
    print(event);
  }));

  sc.add(100);

  // 暂停
  ss.pause();
  // 恢复
  ss.resume();
  // 取消
  ss.cancel();

  // 关闭流
  sc.close();
}

广播

scBroadcast() async {
  StreamController sc = StreamController.broadcast();

  StreamSubscription ss1 = sc.stream.listen(print);
  StreamSubscription ss2 = sc.stream.listen(print);

  sc.addStream(Stream.fromIterable([1, 2, 3, 4, 5]));

  await Future.delayed(Duration(seconds: 2));
  sc.close();
}

StreamTransformer 流转换

scTransformer() async {
  StreamController sc = StreamController<int>.broadcast();

  StreamTransformer stf = StreamTransformer<int, double>.fromHandlers(
    handleData: (int data, EventSink sink) {
      sink.add((data * 2).toDouble());
    },
    handleError: (error, stacktrace, sink) {
      sink.addError('wrong: $error');
    },
    handleDone: (sink) {
      sink.close();
    },
  );

  Stream stream = sc.stream.transform(stf);
  stream.listen(print);
  stream.listen(print);

  sc.addStream(Stream<int>.fromIterable([1, 2, 3, 4, 5]));

  await Future.delayed(Duration(seconds: 2));
  sc.close();
}

执行


main(List<String> args) async {
  print('--- start ---');

  // await periodic();
  // await fromFuture();
  // await fromFutures();

  // await listen();
  // await boardcast();
  // await opt();

  // await scListen();
  // await scBroadcast();
  // await scTransformer();

  print('--- end ---');
}


© 猫哥

ducafecat.tech

ducafecat.gitee.io