0

I am using Geolocator plugin in Flutter Project. I have added it Pubspecs.yaml file also added the permission in AndroidManifest file.

Pubspecs.yaml

dependencies:
  http: ^0.12.0
  flutter:
    sdk: flutter
  shimmer: ^1.0.1
  font_awesome_flutter: ^8.8.1
  geocoder: ^0.2.1
  geolocator: ^5.3.1

Android Manifest

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I am using SDK 28 - compileSdkVersion 28

Here is the code for location access.

String _locationMessage = ""; 

  void _getCurrentLocation() async {
    print('location');
    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position);

    setState(() {
      _locationMessage = "${position.latitude}, ${position.longitude}";
    });

  }

When I call _getCurrentLocation() on Button Pressed. It is showing dialog box to allow location access in App with three option.

Allow all the time
Keep While-In-Use Access
Keep and Don't Ask Again

After selecting Keep While-In-Use Access it is giving below message in Debug console.

E/location_permissions( 7592): Flutter result object is null.

What am I doing wrong?

Also, I want to know will it ask all the times when we try to access location or it will be one time?

Edit

If I select Allow all the time then my app stop responding. I mean if press the button nothing happen. Also I need to uninstall it from phone and run it again then it start giving the prompt for permission.

Edit 2

One strange thing I found. If location service is stop/not enabled then I am getting null. If I manually enable location then it start giving me location.

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • As you pointed in edit 2, even if your app has location permission available and your device location is turned off; you won't get any location data, you get null data (That's what happening). The permission will ask once (unless you select Deny in Android). I think the freezing of app has nothing to do with allow always, it's probably something else you are doing after that. (please post the button code and the place where you are using `_locationMessage`) – Midhun MP Apr 28 '20 at 15:05
  • Thanks for the comments Midhun. I found the reason it will return null if location services is stopped. First need to enable that then it will work. Now, I am trying to find how can i use this null value to show prompt to users to enable location services. – Roxx Apr 28 '20 at 17:50

1 Answers1

0

I think it has something to do with the package that you are using (geolocator), I've also faced the same issue, when I was following a lecture from youtube

so I've gone for some other popular package to get location and it's documentation is very simple.

shaheemMPM
  • 1
  • 1
  • 2