How to implement GPS Status change listener in flutter
1,180
You can use the Location Permissions package to achieve this.location_permissions
Stream <bool> locationEventStream;
@override
void initState() {
locationEventStream = LocationPermissions().serviceStatus.map((s) => s == ServiceStatus.enabled ? true : false);
}
@override
Widget build(BuildContext context) {
return StreamBuilder < bool > (
stream: locationEventStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active && snapshot.hasData) {
if (snapshort.data) {
//GPS on
} else {
//GPS off
}
}
}
}
Comments
-
kriss over 1 year
I was in a FLUTTER project that's related to OLA, UBER, so i need a stream function(like flutter GeoLocatior().isLocationServiceEnabled()) that notifies when user turns off location.
-
Esen Mehmet over 4 yearsBoth location and geolocator package does not have that functionality. You can simple create interval methods.
-
kriss over 4 yearsyeah i too encountered, so making interval is the only way ...?? through method channel....!!!
-
Esen Mehmet over 4 yearsYou don't need method channel, just create a interval for isLocationEnabled or any ready method from packages that's all. You can use Stream or Timer, it depends on your code structure.
-
-
shuster almost 2 yearslocation_permissions package is deprecated now.