flutter - How to turn on or ask user to turn the location on?

Viewed 20303

Hi as I wrote in the title how can I achieve this? I've got the location permission but can not turn the location on!

6 Answers

It will open setting. so user can enable it. it is best practice as per my knowledge.

Install plugin :

https://pub.dartlang.org/packages/android_intent#-installing-tab-

Import in your dart :

import 'package:android_intent/android_intent.dart';

Add method :

void openLocationSetting() async {
    final AndroidIntent intent = new AndroidIntent(
      action: 'android.settings.LOCATION_SOURCE_SETTINGS',
    );
    await intent.launch();
  }

Invoke it done...

I'll be using the things I learnt from this link to guide you.

Add this dependency to pubspec.yaml

dependencies:
  location: ^3.0.0

Now, import the following package to your .dart file

import 'package:location/location.dart';

Now inside a function, to ask for the user's location using a pop-up box, you can do-

Location location = new Location();
bool _serviceEnabled;
LocationData _locationData;

The above were the declarations that we will be using in our following code -

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    debugPrint('Location Denied once');
  }
}

Now, you can use the above snippet as per your requirement to make Location request calls using a pop-up for whatever number of times you want.

If the user's locations has been granted, you can use the following line to store and use the location data.

_locationData = await location.getLocation();

If the location is not granted, then the above line would result in the program failing, so be sure to only use the above line when the user has allowed location access from the pop-up.

Hope this helps. Cheers!

flutterlocation Plugin shows you the dialogue to turn on gps as like native android

First add this dependency in pubspec.yaml:

location: ^1.4.0

Then, use this function to retrieve current location of your device:

import 'package:location/location.dart';
 fetchCurrentLocation() async {

  print("STARTING LOCATION SERVICE");
  var location = Location();
  location.changeSettings(accuracy: LocationAccuracy.POWERSAVE,interval: 1000,distanceFilter: 500);
  if (!await location.hasPermission()) {
    await location.requestPermission();
  }

  try {
    await location.onLocationChanged().listen((LocationData currentLocation) {
      print(currentLocation.latitude);
      print(currentLocation.longitude);
      latitude = currentLocation.latitude;
      longitude = currentLocation.longitude;
    });
  } on PlatformException {
    location = null;
  }

}

You can use System Dialog and ask user to enable it. Check this link for your reference.

Create a platform specific channel and call it when user want to enable it.

I did the same but sometimes pop-up notification of location permission doesn't appear.

I add these lines in the following location:
project/android/app/src/main/AndroidManifest.xml

Related