How can I check if the location and internet services are enabled in Flutter and retrieve their current state?

51 Views Asked by At

I need to ensure both the user's location and internet are enabled during the application usage.

While I've successfully implemented connectivity checking using the internet_connection_checker_plus package, I'm facing challenges in dynamically checking the status of the location service using GeoLocator. Is there a single package or method that allows me to efficiently check and manage both location and internet services? If not, how can I dynamically track the real-time status of the location service using GeoLocator to determine whether it is active or inactive?

1

There are 1 best solutions below

0
FaBotch On

From Geolocator documentation you can use

Future<Position> getUserLocation() async {
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // Location services are not enabled don't continue
      // accessing the position and request users of the
      // App to enable the location services.
      return Future.error('Location services are disabled.');
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        // Permissions are denied, next time you could try
        // requesting permissions again (this is also where
        // Android's shouldShowRequestPermissionRationale
        // returned true. According to Android guidelines
        // your App should show an explanatory UI now.
        return Future.error('Location permissions are denied');
      }
    }

    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }

    // When we reach here, permissions are granted and we can
    // continue accessing the position of the device.
    return await Geolocator.getCurrentPosition();
  }

With riverpod you can declare a provider to check the status of permission

final locationProvider = FutureProvider<Position>((ref) async {
  final controller = ref.read(locationControllerProvider);
  return await controller.getUserLocation();
});

then in your build method, you can watch this provider and react to the status change

Widget build(BuildContext context) {
    final locationCheck = ref.watch(locationProvider);
    return Scaffold(
      body: locationCheck.when(
        data: (locationData) {
          //use location
        },
        loading: () => LoaderWidget(),
        error: (error, stackTrace) {
          // message to the user that location permission is disable
        },
      ),
    );
  }

if you need a real time you can use a StreamProvider instead of a FutureProvider