0

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.

Vincenzo
  • 1
  • 2

1 Answers1

0

[RESOLVED] The problem was the emulator.

Launching the app in the emulator I wasn't able to get the location. I tried to launch the app on a physical device (with Android) and it worked. Probably the problem was that the emulator's was deprecated.

Vincenzo
  • 1
  • 2