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
     }
    }
   }
  }
Share:
1,180
kriss
Author by

kriss

newbie in android developing....

Updated on December 14, 2022

Comments

  • kriss
    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
      Esen Mehmet over 4 years
      Both location and geolocator package does not have that functionality. You can simple create interval methods.
    • kriss
      kriss over 4 years
      yeah i too encountered, so making interval is the only way ...?? through method channel....!!!
    • Esen Mehmet
      Esen Mehmet over 4 years
      You 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
    shuster almost 2 years
    location_permissions package is deprecated now.