Is there a way to find out in Flutter if the GPS is activated or deactivated? I use the plugin location however there I get only the location and not the state of the GPS.
6 Answers
Update: (Geolocator 8.0.1)
bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();
Previous solution:
Accepted answer uses outdated plugin, you can use Geolocator plugin,
var geoLocator = Geolocator();
var status = await geoLocator.checkGeolocationPermissionStatus();
if (status == GeolocationStatus.denied)
// Take user to permission settings
else if (status == GeolocationStatus.disabled)
// Take user to location page
else if (status == GeolocationStatus.restricted)
// Restricted
else if (status == GeolocationStatus.unknown)
// Unknown
else if (status == GeolocationStatus.granted)
// Permission granted and location enabled

- 237,138
- 77
- 654
- 440
-
6Thanks, but how do you display the activation dialog? – mik Mar 04 '19 at 16:41
-
I am looking for the dialog box also. But seems like flutter does not provide it with any function. – Yunus May 19 '19 at 10:32
-
@mik you will have to do that natively, this is the [solution](https://stackoverflow.com/a/33254073/6618622) for Android. – CopsOnRoad May 19 '19 at 11:03
-
1@Yunus You will have to do it natively, see above link. – CopsOnRoad May 19 '19 at 11:03
-
This library helps navigate to system settings https://pub.dev/packages/app_settings – Raj Yadav Jun 23 '19 at 06:38
-
@RajYadav You don't even need special library to open settings, `geolocator` does that for you – CopsOnRoad Jun 23 '19 at 07:00
Using the latest version of Geolocator 5.0
var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
I use this method to check and enable the Gps if disabled.
Future _checkGps() async {
if (!(await Geolocator().isLocationServiceEnabled())) {
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Can't get gurrent location"),
content:
const Text('Please make sure you enable GPS and try again'),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
final AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
intent.launch();
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
},
);
}
}
}

- 237,138
- 77
- 654
- 440

- 74,687
- 32
- 99
- 138
-
3Thanks a lot. This comment saved me. Those who are wondering, AndroidIntent() is from package called android_intent. – Saad Ismail May 29 '19 at 22:25
-
Wow, you guys are unexpectedly helpful. Thanks a bunch for AndroidIntent too. – VipiN Negi Nov 22 '19 at 10:04
-
-
with this one package and one line of code you can open location settings in both platforms. AppSettings.openLocationSettings(). https://pub.dev/packages/app_settings – SardorbekR Jul 09 '20 at 07:10
Update 2019/10/25
The location package now has a function (serviceEnabled()
) to detect whether the location service is enabled and returns a bool as described in its docs and shown in the example:
bool serviceStatus = await _locationService.serviceEnabled();
if (service) {
// service enabled
} else {
// service not enabled, restricted or permission denied
}
Old answer (package outdated)
With geolocations you can check wether the location service is operational. It generally includes more customizability then the location package.
final GeolocationResult result = await Geolocation.isLocationOperational();
if(result.isSuccessful) {
// location service is enabled, and location permission is granted
} else {
// location service is not enabled, restricted, or location permission is denied
}

- 5,767
- 3
- 37
- 47
-
https://pub.dev/packages/geolocation DART 2 INCOMPATIBLE with geolocation 0.2.1 – M.ArslanKhan Oct 25 '19 at 08:28
-
1@M.ArslanKhan thanks for mentioning. I updated the answer to use the latest location package as initially wanted by the OP. – Bostrot Oct 25 '19 at 09:06
Use catchError method with Geolocator, you don't have to use location pub
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
// show location
});
}).catchError((e) {
return Future.error('Location services are disabled.');
});

- 91
- 1
- 8
@humazed thanks for your answer, it's a modification of @humazed's answer with null-safety and Dart 3
Pub: Geolocator and AndroidIntent
// Check the GPS is on
Future _checkGps() async {
if (!(await Geolocator.isLocationServiceEnabled())) {
if (!mounted) return;
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Can't get current location"),
content:
const Text('Your GPS is turn off, please turn it on first.'),
actions: <Widget>[
TextButton(
child: const Text('Turn On'),
onPressed: () async {
const AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
await intent.launch();
if (!mounted) return;
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
},
);
}
}
}

- 1,153
- 2
- 17
- 37
checkLocationPermission() async {
final status = await Permission.location.request();
if (status == PermissionStatus.granted) {
debugPrint('Permission granted');
bool isLocationEnabled = await Geolocator.isLocationServiceEnabled();
if (isLocationEnabled) {
// location service is enabled,
} else {
// open Location settings
await Geolocator.openLocationSettings();
}
} else if (status == PermissionStatus.denied) {
debugPrint(
'Permission denied. Show a dialog and again ask for the permission');
} else if (status == PermissionStatus.permanentlyDenied) {
debugPrint('Take the user to the settings page.');
await openAppSettings();
}
}

- 3,198
- 3
- 18
- 45