calling to StreamController.addError after calling StreamController.add gives error

72 Views Asked by At

I'm trying to forward a stream results to StreamController, however on calling the 3rd future on the example, the StreamController throws an error with unhelpful message.

This is replicable when I try to call addError after calling add with values.

Am I missing something?

test("stream controller", () async {
    StreamController<int> c = StreamController();

    Future<int> success() async {
      return 1;
    }

    Future<int> throwing() async {
      throw Exception();
    }

    Stream<int>.fromFutures([
      throwing(), // 1
      success(), // 2
      throwing(), // 3
    ]).listen((event) {
      c.add(event);
    }, onError: (e, st) {
      c.addError(e, st);
    });

    await expectLater(
      c.stream,
      emitsInOrder([
        emitsError(isA<Exception>()),
        1,
        emitsError(isA<Exception>()),
      ]),
    );
  });

Error:

Exception
test/poll_stream_test.dart 115:7  main.<fn>.throwing
test/poll_stream_test.dart 121:7  main.<fn>
===== asynchronous gap ===========================
dart:async                        _StreamController.addError
test/poll_stream_test.dart 125:9  main.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                        _StreamImpl.listen
test/poll_stream_test.dart 122:8  main.<fn>

I have no idea why this doesn't works, but I'm expecting the StreamController to successfully emits error after I emits some value.

0

There are 0 best solutions below