ReactiveX / rxdart

The Reactive Extensions for Dart

Home Page:http://reactivex.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

switch_map.dart - NoSuchMethodError: The method 'listen' was called on null

larssn opened this issue · comments

commented

Our issue tracker has caught several errors over a few months, where mappedStream below, is null.

_mapperSubscription = mappedStream.listen(

Causes the following crash:

NoSuchMethodError: The method 'listen' was called on null.
Receiver: null
Tried calling: listen()
  File "switch_map.dart", line 25, in _SwitchMapStreamSink.onData
  File "zone.dart", line 1436, in _rootRunUnary
  File "zone.dart", line 1335, in _CustomZone.runUnary
  File "zone.dart", line 1244, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 341, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "stream_impl.dart", line 1129, in _MultiStreamController.addSync
  File "forwarding_stream.dart", line 135, in _MultiControllerSink.add
  File "start_with.dart", line 12, in _StartWithStreamSink.onData
  File "zone.dart", line 1436, in _rootRunUnary
  File "zone.dart", line 1335, in _CustomZone.runUnary
  File "zone.dart", line 1244, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 341, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "broadcast_stream_controller.dart", line 386, in _SyncBroadcastStreamController._sendData.<fn>
  File "broadcast_stream_controller.dart", line 323, in _BroadcastStreamController._forEachListener
  File "broadcast_stream_controller.dart", line 385, in _SyncBroadcastStreamController._sendData
  File "broadcast_stream_controller.dart", line 244, in _BroadcastStreamController.add
  File "subject.dart", line 141, in Subject._add
  File "subject.dart", line 135, in Subject.add
  File "zone.dart", line 1436, in _rootRunUnary
  File "zone.dart", line 1335, in _CustomZone.runUnary
  File "zone.dart", line 1244, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 341, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "stream_pipe.dart", line 123, in _ForwardingStreamSubscription._add
  File "stream_pipe.dart", line 455, in _DistinctStream._handleData
  File "stream_pipe.dart", line 153, in _ForwardingStreamSubscription._handleData
  File "zone.dart", line 1436, in _rootRunUnary
  File "zone.dart", line 1335, in _CustomZone.runUnary
  File "zone.dart", line 1244, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 341, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "stream_controller.dart", line 733, in _SyncStreamControllerDispatch._sendData
  File "stream_controller.dart", line 607, in _StreamController._add
  File "stream_controller.dart", line 554, in _StreamController.add
  File "zone.dart", line 1436, in _rootRunUnary
  File "zone.dart", line 1335, in _CustomZone.runUnary
  File "zone.dart", line 1244, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 341, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 591, in _DelayedData.perform
  File "stream_impl.dart", line 706, in _StreamImplEvents.handleNext
  File "stream_impl.dart", line 663, in _PendingEvents.schedule.<fn>
  File "zone.dart", line 1420, in _rootRun
  File "zone.dart", line 1328, in _CustomZone.run
  File "zone.dart", line 1236, in _CustomZone.runGuarded
  File "zone.dart", line 1276, in _CustomZone.bindCallbackGuarded.<fn>
  File "zone.dart", line 1428, in _rootRun
  File "zone.dart", line 1328, in _CustomZone.run
  File "zone.dart", line 1236, in _CustomZone.runGuarded
  File "zone.dart", line 1276, in _CustomZone.bindCallbackGuarded.<fn>
  File "schedule_microtask.dart", line 40, in _microtaskLoop
  File "schedule_microtask.dart", line 49, in _startMicrotaskLoop

Not sure how to reproduce it, since there's none of our code in the stack trace.

Rxdart version: 0.27.2

Is null-safety enable in your project?
Because mappedStream has type Stream<T>, it cannot be null

commented

Null safety is not enabled, but that shouldn't matter as far as I know, we get plenty of null cast errors from other plugins, or flutter itself.

But yeah the error is weird, it shouldn't be nullable, but it clearly was or the crash wouldn't happen.

Check your use: startWith(...).switchMap((_) => /*check null here*/)

commented

Don't have any of those, whats your thinking?

Don't have any of those, whats your thinking?

I think you have a null return inside a switchMap

commented

I assume this is allowed:

final userStream = _a
        .authStateChanges()
        .switchMap((usr) {
          if (usr == null) return Stream<MyUser/*?*/>.value(null);

            ...
        });

I assume this is allowed:

final userStream = _a
        .authStateChanges()
        .switchMap((usr) {
          if (usr == null) return Stream<MyUser/*?*/>.value(null);

            ...
        });

Yes

commented

We haven't seen this error in a very long time. So I'm gonna close it. Thanks @hoc081098