please take a look to this code =>

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');
  }
}

am I using distinct() the wrong way?

thank you in advance


Solution 1: Francesco Iapicca

nevermind I just put distinct in the wrong place, the following works as intended

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');
  }
}