Yukams / background_locator_fixed

A Flutter plugin for updating location in background.
MIT License
50 stars 94 forks source link

_TypeError (Null check operator used on a null value) #100

Closed jabguru closed 8 months ago

jabguru commented 1 year ago

I get this error on both Android and iOS

Screen Shot 2023-09-01 at 2 35 03 PM Screen Shot 2023-09-01 at 2 35 27 PM Screen Shot 2023-09-01 at 2 35 38 PM Screen Shot 2023-09-01 at 2 35 48 PM Screen Shot 2023-09-01 at 2 35 59 PM
Myzel394 commented 1 year ago

I had a similar issue with the notificationTapCallback argument. I had to add the @pragma("vm:entry-point") decorator. Can you send the full code for the registerLocationUpdate function?

jabguru commented 1 year ago

@Myzel394 thanks for your response

registerLocationUpdate is a method in the package.

Myzel394 commented 1 year ago

@Myzel394 thanks for your response

registerLocationUpdate is a method in the package.

I mean your code :D Can you send your code / how do you call this function?

jabguru commented 1 year ago

@Myzel394 that's what is in the first image, or should I resend the whole thing?

Myzel394 commented 1 year ago

@Myzel394 that's what is in the first image, or should I resend the whole thing?

yes please, just paste the content itself

jabguru commented 1 year ago
  Future<void> startLocationService() async {
    if (accountService.userCheck(userType: UserType.courier) &&
        currentScreenService.locationChangeLocationService == null) {
      //? calling this to get permission
      await locationService.getUserLocation();

      // TODO: THERE'S AN ERROR HERE
      await BackgroundLocator.registerLocationUpdate(
        updateLocationCallback,
        autoStop: false,
        iosSettings: const IOSSettings(
          accuracy: LocationAccuracy.NAVIGATION,
          distanceFilter: 200,
        ),
        androidSettings: AndroidSettings(
          accuracy: LocationAccuracy.NAVIGATION,
          interval: 5,
          distanceFilter: 200,
          androidNotificationSettings: AndroidNotificationSettings(
            notificationChannelName: 'Location tracking',
            notificationTitle: 'bg_location_notif_title'.tr(),
            notificationMsg: 'bg_location_notif_msg'.tr(),
            notificationBigMsg: 'bg_location_notif_big_msg'.tr(),
            notificationIconColor: kColorScheme.primaryColor,
            notificationIcon: 'notif_icon',
          ),
        ),
      );
    }
  }

@Myzel394

Myzel394 commented 1 year ago

What does your updateLocationCallback look like?

jabguru commented 1 year ago
 Future<void> updateLocationCallback(LocationDto locationDto) async {
    await setupLocator(fromIsolate: true);

    if (accountService.userCheck(userType: UserType.courier) &&
        currentScreenService.locationChangeLocationService == null) {
      try {
        double lat = locationDto.latitude;
        double lng = locationDto.longitude;

        if (accountService.loggedInUser!.courier!.currentPositionLatitude !=
                lat ||
            accountService.loggedInUser!.courier!.currentPositionLongitude !=
                lng) {
          Map data = {
            'current_position_latitude': lat.toString(),
            'current_position_longitude': lng.toString(),
          };

          await accountRepository.updateCourier(data);
          log('Courier location updated: $lat, $lng');
        } else {
          log('Courier\'s position didn\'t change: $lat, $lng');
        }
      } catch (e) {
        log('Error updating courier location: $e');
      }
    }
  }

@Myzel394

jabguru commented 1 year ago

So I should just add the decorator @pragma("vm:entry-point") To the function?

@Myzel394

Myzel394 commented 1 year ago

@jabguru yes exactly, sorry, I thought I wrote you an answer already but seems like I forgot to actually send the comment xD

santojc commented 8 months ago

@jabguru @Myzel394 whats the answer again? im blank here

jabguru commented 8 months ago

@santojc its to add @pragma("vm:entry-point") ontop of your callback method.

I didn't have to do right now though, it's working on it's own again.

jabguru commented 8 months ago

I'll be closing the issue, it's working again.

Myzel394 commented 8 months ago

@santojc it's exactly what @santojc said; it worked for me back then, but I don't know whether it will work now