Turn on location services without navigating to settings page ? Flutter Dart

1,627

You can use the location package, you just have to add the required permissions in your AndroidManifest.xml and Info.plist files, permission will be asked in a pop-up.

Share:
1,627
TSR
Author by

TSR

Updated on December 09, 2022

Comments

  • TSR
    TSR over 1 year

    We are migrating from to Flutter. We used this thread to Turn on location services without navigating to settings page

    How can this be achieved in Flutter?

    The current temporary code that navigates to settings:

      Future _getCurrentLocation() async {
        Position position;
        try {
          position = await Geolocator().getCurrentPosition(
              desiredAccuracy: LocationAccuracy.bestForNavigation);
        } on PlatformException catch(e){
          print(e.code);
          if(e.code =='PERMISSION_DISABLED'){
            print('Opening settings');
            await openLocationSetting();
            try {
              position = await Geolocator().getCurrentPosition(
                  desiredAccuracy: LocationAccuracy.bestForNavigation);
            } on PlatformException catch(e){
              print(e.code);
            }
          }
        }
    
        setState(() {
    //      _center = LatLng(currentLocation['latitude'], currentLocation['longitude']);
          _center = LatLng(position.latitude, position.longitude);
        });
      }
      Future openLocationSetting() async {
        final AndroidIntent intent = new AndroidIntent(
          action: 'android.settings.LOCATION_SOURCE_SETTINGS',
        );
        await intent.launch();
    
      }
    
  • MJ Montes
    MJ Montes almost 5 years
    enabling Location Permissions and Locations Services are 2 different things