Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In flutter how i can check if location is enabled?

Tags:

flutter

I have problem when user entered google map page without location enabled the map wont updated so i want to check if the user have location enabled beforehand so i need function that return true if location enabled and false if not.

like image 581
Michael2411 Avatar asked Dec 08 '22 13:12

Michael2411


2 Answers

use permission handler plugin

ServiceStatus serviceStatus = await PermissionHandler().checkServiceStatus(PermissionGroup.location);
bool enabled = (serviceStatus == ServiceStatus.enabled);

you can also show a rationale for requesting permission (Android only)

bool isShown = await PermissionHandler().shouldShowRequestPermissionRationale(PermissionGroup.location);

or Location Plugin

var location = Location();
bool enabled = await location.serviceEnabled();

and request to enable it by

bool gotEnabled = await location.requestService();
like image 95
Mohamed Ali Avatar answered Jan 17 '23 19:01

Mohamed Ali


for those who came to this stackoverflow question like myself, there have been changes and updates on PermissionHandler package and the older solutions are now invalid.

you can check if the location service is enabled:

if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
  // Use location.
}
like image 28
DNS Avatar answered Jan 17 '23 18:01

DNS