请看一下这段代码=>

void main() async {
  Stream<int> _intStream() async* {
    for (int i = 0; i < 10; i++) yield i;
  }

  Stream<int> _even() => _intStream().where((int i) => i % 2 == 0);
  Stream<bool> _isEven(Stream<int> stream) => stream.map((int i) => i % 2 == 0);

  print('test 1: should print 5 [true] and 5 [false] alternate');
  await for (bool b in _isEven(_intStream())) {
    print('test 1: $b');
  }

  print('test 2: should print 5 [true]');
  await for (bool b in _isEven(_even())) {
    print('test 2: $b');
  }

  print('test 3: should print ONLY ONE [true]');
  await for (bool b in _isEven(_even().distinct())) {
    print('test 3: $b');
  }
}
我使用distinct()的方式错误吗?
先感谢您

最佳答案

没关系,我只是把错误放在错误的地方,
以下作品按预期进行

void main() async {
  Stream<int> _intStream() async* {
    for (int i = 0; i < 10; i++) yield i;
  }

  Stream<int> _even() => _intStream().where((int i) => i % 2 == 0);
  Stream<bool> _isEven(Stream<int> stream) => stream.map((int i) => i % 2 == 0);

  print('test 1: should print 5 [true] and 5 [false] alternate');
  await for (bool b in _isEven(_intStream())) {
    print('test 1: $b');
  }

  print('test 2: should print 5 [true]');
  await for (bool b in _isEven(_even())) {
    print('test 2: $b');
  }

  print('test 3: should print ONLY ONE [true]');
  await for (bool b in _isEven(_even()).distinct()) {
    print('test 3: $b');
  }
}

07-25 21:00