permissions_utils.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import 'package:geolocator/geolocator.dart';
  2. class PermissionsUtils {
  3. /// Get Location permissions of the device
  4. ///
  5. /// Returns a [Future.error] if Location permission is denied
  6. static Future<void> determineLocationPermission() async {
  7. bool serviceEnabled;
  8. LocationPermission permission;
  9. // Test if location services are enabled.
  10. serviceEnabled = await Geolocator.isLocationServiceEnabled();
  11. if (!serviceEnabled) {
  12. // Location services are not enabled don't continue
  13. // accessing the position and request users of the
  14. // App to enable the location services.
  15. return Future.error('Location services are disabled.');
  16. }
  17. permission = await Geolocator.checkPermission();
  18. if (permission == LocationPermission.denied) {
  19. permission = await Geolocator.requestPermission();
  20. if (permission == LocationPermission.denied) {
  21. // Permissions are denied, next time you could try
  22. // requesting permissions again (this is also where
  23. // Android's shouldShowRequestPermissionRationale
  24. // returned true. According to Android guidelines
  25. // your App should show an explanatory UI now.
  26. return Future.error('Location permissions are denied');
  27. }
  28. }
  29. if (permission == LocationPermission.deniedForever) {
  30. // Permissions are denied forever, handle appropriately.
  31. return Future.error('Location permissions are permanently denied, we cannot request permissions.');
  32. }
  33. }
  34. }