'Flutter Location service no response

I am delevoping an app with gMaps and in my application I am getting location permission from user and after that I am requesting the user to turn his/her gps on. The problem is when the gps is turned on for the first time, Application is not able to get the location data.

This is where my program is stuck:

_locationData = await location.getLocation();
    print(_locationData.latitude);

After calling this lines, there is no message in the logcat. I think I am not able to get the location data from the provider.

void getPermissions() async {
    Location location = new Location();

    bool _serviceEnabled;
    PermissionStatus _permissionGranted;

    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        print('whats happening!');
        return;
      }
    }

    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      print('hahahahahahahahahahahahahah' + _serviceEnabled.toString());
      if (!_serviceEnabled) {
        return;
      }
    }

    _locationData = await location.getLocation();
    print(_locationData.latitude);
   
    controller.animateCamera(CameraUpdate.newLatLng(
        new LatLng(_locationData.latitude, _locationData.longitude)));

}


Solution 1:[1]

   serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      Location loc = Location();
      serviceEnabled = await loc.requestService();

      if (!serviceEnabled) {
        return Future.error('Location services are disabled.');
      }
    }

geolocator: ^7.7.1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmet Aziz Be?li