首页 > 建站教程 > dart >  dart笔记18:stream流的transform转换正文

dart笔记18:stream流的transform转换

dart笔记18:stream流的transform转换,将整数型转换成了描述性的字符串型:
import 'dart:async';

void main() {
  testTransformStream();
}

testTransformStream() async {
  Stream stream = Stream<int>.fromIterable([111111, 222222, 333333]);

  //将数字型转成字符串行
  var st =
      StreamTransformer<int, String>.fromHandlers(handleData: (int data, sink) {
    if (data == 333333) {
      sink.add('密码输入正确');
    } else {
      sink.add('密码输入错误');
    }
  });

  stream.transform(st).listen((data) {
    print(data);
  }, onError: () => print('发生错误'));
}