// init background geolocation
void initGeolocation() async {
debugPrint('geo');
_geoSubscription = Geolocation.locationUpdates(
accuracy: LocationAccuracy.best,
displacementFilter: 10.0, // in meters
inBackground:
true, // by default, location updates will pause when app is inactive (in background). Set to true to continue updates in background.
).listen((result) {
debugPrint('result');
debugPrint(result.dataToString());
if (result.isSuccessful) {
// todo with result
_latitude = result.location.latitude;
_longitude = result.location.longitude;
debugPrint(result.dataToString());
}
});
}
Plugin doesn't work, i use the forked plugin by shehabic https://github.com/shehabic/geolocation for Dart 2 compatibility
this is the error that return
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception: NoSuchMethodError: The getter 'last' was called on null. Receiver: null Tried calling: last
0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
1 LocationResult.location (file:///Library/flutter/.pub-cache/git/geolocation-5f7e2ec8a00fa0c020791c5b0c1844b2bd320c05/lib/data/location_result.dart:27:38)
2 LocationResult.dataToString (file:///Library/flutter/.pub-cache/git/geolocation-5f7e2ec8a00fa0c020791c5b0c1844b2bd320c05/lib/data/location_result.dart:31:12)
3 _MyHomePageState.initGeolocation. (package:sequi/main.dart:77:25)
4 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
5 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
6 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
7 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
8 _PendingEvents.schedule. (dart:async/stream_impl.dart<…>
my code is very simple,
// init background geolocation void initGeolocation() async { debugPrint('geo'); _geoSubscription = Geolocation.locationUpdates( accuracy: LocationAccuracy.best, displacementFilter: 10.0, // in meters inBackground: true, // by default, location updates will pause when app is inactive (in background). Set to
true
to continue updates in background. ).listen((result) { debugPrint('result'); debugPrint(result.dataToString()); if (result.isSuccessful) { // todo with result _latitude = result.location.latitude; _longitude = result.location.longitude; debugPrint(result.dataToString()); } }); }