I'm working with Google maps API on Flutter and Geolocator.getCurrentPosition never returns. This is my code (mostly taken from Flutter Projects by Simone Alessandria and some changes taken from the web trying to fix this issue)
Future _getCurrentLocation() async {
bool isGeolocationAvailable = await Geolocator.isLocationServiceEnabled();
if (isGeolocationAvailable) {
try {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best, timeLimit: Duration(seconds: 10)).then((pos) {
setPosition(pos);
});
} catch (error) {
print(error.toString());
Geolocator.getLastKnownPosition().then((pos) {
setPosition(pos);
});
}
}
return null;
}
As I mentioned Geolocator.getCurrentPosition never returns, with the time limit at least I get an exeption.
Here's my Flutter doctor just in case:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.1, on Ubuntu 20.04.3 LTS 5.11.0-38-generic, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] Android Studio (version 4.1)
[✓] VS Code
[✓] Connected device (1 available)
• No issues found!
And the versions
environment:
sdk: ">=2.14.2 <3.0.0"
dependencies:
flutter:
sdk: flutter
http: ^0.13.3
google_maps_flutter: ^2.0.10
permission_handler: ^8.1.6
geolocator: ^7.7.0
I'm running in the Pixel emulator
Thanks!