Location permission in flutter | Enable location in flutter



App Location permission



To use location in the app you need below steps

1. Request App to access location
2. Request to enable GPS


1.Location Permission

You can use below dependency or a latest version of this dependency. This permission is for the app to access location


  location_permissions: '^2.0.0'

  import 'package:location_permissions/location_permissions.dart';

 Checking location permission

  PermissionStatus permission = await     
  LocationPermissions().checkPermissionStatus();

 

 To request the location permission

  LocationPermissions().requestPermissions() 








2. Request to enable GPS


 To Request to enable GPS you need to add below dependency (check for latest   version)


   location: '^ 2.3.5'

   import 'package:location/location.dart';


First you need a location class object and with that you have to request a location service which returns a Future bool value as a location status.


   var location = new Location();
    return await location.requestService().then((onValue){
      return onValue;
    });












Now to better understand below is the code.



onPressed: () async {
  if (await checkAppPermission(context)) {
    if (await accessSystemLocation()) {
      /// Location enabled success
    }
  }
});


/// Asking location permission of app


  static Future<bool> checkAppPermission(BuildContext context)  async {
    PermissionStatus permission = await LocationPermissions().checkPermissionStatus();
    if(permission!=PermissionStatus.granted && await LocationPermissions().requestPermissions()!=PermissionStatus.granted) {
      return false;
    }
    return true;
  }


 /// Asking to turn on the device GPS

  static  Future<bool> accessSystemLocation() async {
    var location = new Location();
    return await location.requestService().then((onValue){
      return onValue;
    });
  }








Previous
Next Post »