Unhandled Exception: PlatformException(ERROR_GEOCODING_INVALID_COORDINATES, Unable to find an address for the supplied coordinates., null)
💬 Questions and Help
I have been getting the above error whenever i try geocoding, here's my code. Just to be sure i do get my co-ordinates(print statements confirms that)
getUserLocation() async {
Position position = await Geolocator()
.getCurrentPosition();
print(position.latitude);
print(position.longitude);
List<Placemark> placemarks = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
print(placemarks);
Placemark placemark = placemarks[0];
print(placemark);
String completeAddress =
'${placemark.subThoroughfare} ${placemark.thoroughfare}, ${placemark.subLocality} ${placemark.locality}, ${placemark.subAdministrativeArea}, ${placemark.administrativeArea} ${placemark.postalCode}, ${placemark.country}';
print(completeAddress);
String formattedAddress = "${placemark.locality}, ${placemark.country}";
var text = formattedAddress;
}
The code doesn't get past the print longitude statement and i get the above error
Aqui está o problema
no arquivo Geolocator (5.1.5), na linha 205
está desta formar
void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido
_handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }
you have to put your request inside a try-catch block like this:
getUserLocation() async {
try {
Position position = await Geolocator()
.getCurrentPosition();
print(position.latitude);
print(position.longitude);
List<Placemark> placemarks = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
print(placemarks);
Placemark placemark = placemarks[0];
print(placemark);
String completeAddress =
'${placemark.subThoroughfare} ${placemark.thoroughfare}, ${placemark.subLocality} ${placemark.locality}, ${placemark.subAdministrativeArea}, ${placemark.administrativeArea} ${placemark.postalCode}, ${placemark.country}';
print(completeAddress);
String formattedAddress = "${placemark.locality}, ${placemark.country}";
var text = formattedAddress;
}
catch (e) {
//handle errors and exceptions here
}
}
because this lib return an exception in case of no results (rather than an empty list, which would make more sense) And unfortunatelly you can't use the then().catchError() pattern either because this is an exception that is thrown by this lib in case of no results and not an error (so it isn't catched by the Future.catchError() pattern meaning you necessarilly need to use try{}catch(e){} to handle the exception )
Aqui está o problema
no arquivo Geolocator (5.1.5), na linha 205
está desta formar
void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido
_handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }
Compiler message: /C:/scr/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.1.5/lib/geolocator.dart:216:14: Error: Can't return a value from a void function. return permission;; ^ Estava na maior esperança que sua tese estava correta, mas o compiler não pensa assim :( Mas foi ótima sua contribuição.
Aqui está o problema no arquivo Geolocator (5.1.5), na linha 205 está desta formar void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }
Compiler message: /C:/scr/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.1.5/lib/geolocator.dart:216:14: Error: Can't return a value from a void function. return permission;; ^ Estava na maior esperança que sua tese estava correta, mas o compiler não pensa assim :( Mas foi ótima sua contribuição.
Funciona sim amigo, o o seu problema é que deixou o void na frente do _handleInvalidPermissions e não pode. Pois o void significa que não retorna nada, como você está retornando alguma coisa, não pode deixar o void.
Da uma olhada no meu código novamente, percebe que não tem o void?
Since version 6.0.0 the geocoding features are no longer part of the geolocator plugin and moved to their own plugin which can be found here.