I am using geolocator: '^3.0.1' permission_handler: '^3.0.0'
Now I want to fetch the current location of the user and show it on the map as the user opens the Map.
So my Code is :
Future<void> requestPermission() async {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.location)
.then((PermissionStatus permissionStatus) async {
print("Checking Permission " + permissionStatus.toString());
if (permissionStatus == PermissionStatus.granted) {
_getCurrentLocation();
} else {
print("Asking Permission " + permissionStatus.toString());
final List<PermissionGroup> permissions = <PermissionGroup>[
PermissionGroup.locationWhenInUse
];
final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
await PermissionHandler().requestPermissions(permissions);
if (PermissionStatus.granted ==
permissionRequestResult[PermissionGroup.locationWhenInUse]) {
print("Permission Granted " + permissionStatus.toString());
_getCurrentLocation();
}
}
});
}
and permissions are defined in the manifest for android and info.list for IOS.
Now the issue is when I run this function and when it calls requestPermission
function, it shows the popup asking for the permission and
when I allow the permission
app crashes with an error :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted
and also the result of permission is Permission.disabled
though I allowed the permission in application settings and in permission it shows that location is permission is allowed. but I tried opening app several times it shows Permission.disabled.
and even if I deny
the app crashes with the same error.
So what I have concluded is :
If I allow
or deny
it crashes because it is requesting multiple times and even if I allow the result is Permission.disabled.
Link for the video: https://youtu.be/A1DKkw6u4HI
Can anyone help me solving this issue?
Or please tell me how to take the current location map easily
Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION .
please tell me how to take the current location map easily
If you just need to fetch current location easily and you just need location permission then :
you can use location
plugin with flutter :
In your pubspec.yml
: location : ^2.3.0
Then for fetching Current location :
Import location Package
import 'package:location/location.dart' as locationPackage;
Add this to your State
locationPackage.Location _locationService = new locationPackage.Location();
bool _permission = false;
Call this function in initState or whenever you need current Location
fetchCurrentLocation() async {
await _locationService.changeSettings(
accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);
locationPackage.LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
print("Location: ${location.latitude}");
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
fetchCurrentLocation();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
//error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
//error = e.message;
}
location = null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With