background_locator icon indicating copy to clipboard operation
background_locator copied to clipboard

Null check operator used on a null value

Open jesussmile opened this issue 3 years ago • 5 comments

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

jesussmile avatar Feb 11 '22 11:02 jesussmile

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

janosroden avatar Feb 18 '22 14:02 janosroden

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

Yes this is the, exact reason for getting this error

Habil24 avatar Mar 31 '22 19:03 Habil24

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?

jesussmile avatar Apr 01 '22 01:04 jesussmile

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(.......));

Habil24 avatar Apr 01 '22 14:04 Habil24

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

jesussmile avatar Apr 01 '22 14:04 jesussmile