igaurab/user_location_plugin

NoSuchMethodError in flutter testing on dispose

Opened this issue · 1 comments

When I use the location plugin in my app, all tests fail with an NoSuchMethodError / Null Pointer Error.

The following NoSuchMethodError was thrown while finalizing the widget tree:
The method 'cancel' was called on null.
Receiver: null
Tried calling: cancel()

When the exception was thrown, this was the stack:
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      _MapsPluginLayerState.dispose (package:user_location/src/user_location_layer.dart:55:42)
#2      StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4435:12)
#3      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1748:13)
[...]

Apparently the _onLocationChangedStreamSubscription listener is never initialized but waiting in the user_location_layer.dart::101 indefinitely for the location permission request.

When disposed in line user_location_layer.dart:55:42 the cancel operation is called, without checking if the subscription ever succeeded.

To Reproduce
Steps to reproduce the behavior:

  1. Implement location plugin in flutter project widget with flutter_map
  2. Write test that starts the widget
  3. Run "flutter test"

Expected behavior
No error, even without location permissions response.

Environment:

  • OS: Ubuntu 18.04
  • user_location Version: 0.1.2
  • flutter_map Version: 0.8.2
  • Flutter Version: 1.12.13
  • Dart Version: 2.7.0

Additional context
The tests are not about testing the plugins functionality or integration. The error appears already from instantiating the widget, without further interaction with the plugin.

A workaround is adding the user plugin to the map only when user location permission has been granted.
Still, would be nice if the plugin could work without throwing erros even with missing user location permission.