Get Current User Location Not Working If GPS not on before app opened

Troubleshooting

I have a page, where I am currently setting the text value as a page state variable. (userLocation)

For the same screen, I have (attached event) happening in the on page load.

  1. Request Permissions (Requesting Location Permission)

  2. enableGPS is to get the user to enable GPS (This wasnt happening on its own when I was testing by exporting apk on android). Code for the same is below
    Future<bool> enableGPS() async {

      Location location = new Location();

      // Ensure location services are enabled

      bool _serviceEnabled = await location.serviceEnabled();

      if (!_serviceEnabled) {

        _serviceEnabled = await location.requestService();

        if (!_serviceEnabled) {

          print("Location services are disabled.");

          return false; // Exit if services are not enabled

        }

      }

      // Wait until a valid location is retrieved

      bool validLocation = false;

      while (!validLocation) {

        var currentLocation = await location.getLocation();

        if (currentLocation.latitude != 0.0 && currentLocation.longitude != 0.0) {

          validLocation = true;

        } else {

          await Future.delayed(

              Duration(seconds: 2)); // Wait for GPS to provide valid location

        }

      }

      print("GPS has been enabled and valid location acquired.");

      return true;

    }

  3. Update Page State, sets the userLocation from Global Property (Current Device Location), with rebuild current page

This page is called from another page on button click.

The problem is that if my GPS is on when starting this screen, the app waits for 4/5 seconds to get the location and then shows current LatLng value.

But if my GPS isnt on, then it will show a popup to enable GPS, and as soon as I click ok on that, it will show the screen with LatLng value set to 0.

I am not sure why is it happening, is my third action nor awaiting on the completion of second action? Is the page rebuild not happening properly?
Any help is appreciated.



What have you tried so far?

Tested app on android by exporting apk because location thing isnt workin on browser anyway.
The documentation says that the global property is supposed to have the value, but it doesnt say when does it update that value, or how to forcefully update the value?

Did you check FlutterFlow's Documentation for this topic?
Yes
2
1 reply