Search code examples
androidflutterbackgroundgeolocationflutter-dependencies

My flutter app is crashing (keep stopping) if user give location permission as "Allow all the time". Problem is only in Android 13 devices


My flutter app is crashing (keep stopping) if user give location permission as "Allow all the time". Problem is only in Android 13 devices, its working in all other versions.

Problem started after upgrading compileSdkVersion 31 targetSdkVersion 31

previously it was 30, it was working fine in 30.

  • flutter version: 1.22.6
  • background_locator: ^1.3.0+1
  • geolocator: ^6.2.1

Added this line in app/build.gradle and tried 2.7.0 and 2.7.1. But stil its not working

dependencies {
    ...
    implementation 'androidx.work:work-runtime:2.7.1'
}

App is crashing at this function

BackgroundLocator.registerLocationUpdate(
            LocationCallbackHandler.callback,
            iosSettings: IOSSettings(
                accuracy: locset.LocationAccuracy.NAVIGATION,
                distanceFilter: 0),
            autoStop: false,
            androidSettings: AndroidSettings(
                accuracy: locset.LocationAccuracy.NAVIGATION,
                wakeLockTime: wakeLockTime,
                interval: interval,
                distanceFilter: 0,
                client: LocationClient.google,
                androidNotificationSettings: AndroidNotificationSettings(
                    notificationChannelName: 'Location tracking',
                    notificationTitle: 'Location Tracking',
                    notificationMsg: 'Track location in background',
                    notificationBigMsg:
                        'Background location is on to keep the app up-to-date with your location. This is required for main features to work properly when the app is not running.',
                    notificationIconColor: Colors.grey,
                    notificationTapCallback:
                        LocationCallbackHandler.notificationCallback)));

Solution

  • Try this

    • permission_handler: ^10.4.2
    • device_info_plus: ^9.0.2

    Example Code :

     _askPermission() async {
    PermissionStatus result;
    if (Platform.isAndroid) {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      if (androidInfo.version.sdkInt >= 33) {
        result = await Permission.locationAlways.request();
      } else {
        result = await Permission.location.request();
      }
    } else {
      result = await Permission.location.request();
    }
    
    if (result.isGranted) {
    
      // your code
       
    } else {
      await openAppSettings();
    }}