skip to Main Content

I am developing a Flutter app, and my initial screen is a custom splash screen. I’m trying to get the user’s location on this screen. If the app gets the user’s location, it will open the home screen widget with the location data as an argument. This argument is nullable, so if I can’t get the location, the home screen will still work fine.

Here is my code to get the location:


if (permissionGranted) {
      try {
        final location = await Geolocator.getCurrentPosition();
        try {
          // do some API call
        } catch (e) {
          print(e);
        }
        
        if (!mounted) return;
        router. go(
          '/',
          extra: LatLng(location.latitude, location.longitude),
        );
      } catch (e) {
        print('Error obtaining location: $e');
        router.go(
          '/',
        );
      }
    } else {
      router.go(
        '/',
      );
    }

I am using go_router for routing and calling this from my splash screen’s init method, which is an async function.

The code works fine on Android and also on a real iPhone SE device. However, when I try to run my app on an iPad Air (simulator) or iPhone 13 mini (simulator) with iOS 17.5.1, it gets stuck on the splash screen forever.

Due to this issue, my app has been rejected twice from the App Store.

Questions:

  • Are there any known issues with the Geolocator package on iOS simulators for iOS 17.5.1?
  • Could this be an issue with my implementation of the go_router?
  • Are there any additional configurations required specifically for iOS simulators that I might be missing?

Any help to debug and solve this issue would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I have solved the issue and successfully published it.

    I believe the problem was that some models of the iPad do not have GPS service. In that case, if you call Geolocator.getCurrentPosition(), it will return a null value.

    I encountered that by creating a custom function. The concept is that it will first call Geolocator.getCurrentPosition() and wait for some time. If it can't get any data, it will then call Geolocator.getLastKnownPosition() and return the value. With this approach, the possibility of getting a null position is low.

    Here is my solution:

    Future<Position?> getUserLocation() async {
      Position? position;
    
      try {
        // Attempt to get the current position with a timeout of 5 seconds
        position = await Geolocator.getCurrentPosition().timeout(
          const Duration(seconds: 5),
          onTimeout: () async {
            unawaited(
              Geolocator.getCurrentPosition()
                  .then((newPosition) {})
                  .catchError((error) {}),
            );
            // If it times out, try to get the last known position
            final Position? lastKnownPosition =
                await Geolocator.getLastKnownPosition();
            // If the last known position is found, initiate a new request for the current position (without waiting for it)
    
            return lastKnownPosition!;
          },
        );
      } catch (e) {
        if (kDebugMode) {
          print("Error obtaining current position: $e");
        }
      }
    
      return position;
    }
    
    

  2. I am experiencing the same issue.

    I resolved the problem of being stuck on the splash screen by adding the NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to the info.plist.

    However, I am still encountering errors with the features that use location information, making it impossible to pass the review.

    🙁


    I resolved the errors by adding a timeout as follows.

    LocationData locationData = await _location.getLocation()
    

    to

    LocationData locationData = await _location.getLocation().timeout(const Duration(seconds: 5));
    

    Although this does not allow the use of location-related features, it will at least prevent the review from being rejected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search