rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
287 stars 321 forks source link

Null check operator used on a null value #312

Open jesussmile opened 2 years ago

jesussmile commented 2 years ago

Hi! I am just trying out this plugin but keep running into the null check issue, here is my code

 void getLocationLiveUpdates() async {

    await BackgroundLocator.initialize();
    await BackgroundLocator.isServiceRunning();

    BackgroundLocator.registerLocationUpdate((LocationDto data) {
      BackgroundLocator.updateNotificationText(
          title: "new location received",
          msg: "${DateTime.now()}",
          bigMsg: "${data.latitude}, ${data.longitude}");
    });
}

The error log

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
E/flutter (27770): #0      SettingsUtil._getCommonArgumentsMap
package:background_locator/utils/settings_util.dart:39
E/flutter (27770): #1      SettingsUtil.getArgumentsMap
package:background_locator/utils/settings_util.dart:17
E/flutter (27770): #2      BackgroundLocator.registerLocationUpdate
package:background_locator/background_locator.dart:37
janosroden commented 2 years ago

I think you can't use lambda callback, use global or static callback instead

Habil24 commented 2 years ago

I think you can't use lambda callback, use global or static callback instead

Yes this is the, exact reason for getting this error

jesussmile commented 2 years ago

I think you can't use lambda callback, use global or static callback instead

Yes this is the, exact reason for getting this error

Hi habil! I am new to flutter and can't implement static or global call back can you please post your code snippet of this implementation?

Habil24 commented 2 years ago

Hi, you just need to declare a static function like below:

static void backgroundLocationCallBack(LocationDto location) {
    // do smth with location data
  }

Then pass it as an initial argument to the registerLocationUpdate function like below:

BackgroundLocator.registerLocationUpdate(backgroundLocationCallBack,
            autoStop: true,
            disposeCallback: backgroundLocationDisposeCallBack,
            iosSettings: iOS.IOSSettings(.......),
            androidSettings: android.AndroidSettings(.......));
jesussmile commented 2 years ago

Hi, you just need to declare a static function like below:

static void backgroundLocationCallBack(LocationDto location) {
    // do smth with location data
  }

Then pass it as an initial argument to the registerLocationUpdate function like below:

BackgroundLocator.registerLocationUpdate(backgroundLocationCallBack,
            autoStop: true,
            disposeCallback: backgroundLocationDisposeCallBack,
            iosSettings: iOS.IOSSettings(.......),
            androidSettings: android.AndroidSettings(.......));

Thank you sir! @Habil24 @janosroden