I'm trying to create a mobile app in Flutter.
I need to get the user location when I press a button. Reading on the Internet, I found some useful examples and I wrote some lines of code following some tutorials too.
Future<void> getLocation() async {
var _serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
var _permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
var currentLocation = await location.getLocation();
setState(() {
current = currentLocation;
string = currentLocation.latitude!.toString() +
' ' +
currentLocation.longitude!.toString();
});
}
With this code I should be able to update the text of a string with the latitude and longitude of the user position.
It doesn't work properly. Can anybody help me?
I'm using Android Studio for launching the app with the emulator.