Skip to content

[Bug]: Geolocator.getCurrentPosition hangs after app restart #1687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 of 8 tasks
corepuncher opened this issue Apr 15, 2025 · 8 comments
Open
3 of 8 tasks

[Bug]: Geolocator.getCurrentPosition hangs after app restart #1687

corepuncher opened this issue Apr 15, 2025 · 8 comments
Assignees

Comments

@corepuncher
Copy link

corepuncher commented Apr 15, 2025

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Steps to reproduce

In an Android app, request GeoLocator.getCurrentPosition().

First time is fast. I use "medium".

Then, either kill the app and re-open, or, do a hot reload if debugging.

Second time, or perhaps one or two times after, the app will hang during getCurrentPosition.

Expected results

It should again work fast after killing the app and restarting.

Actual results

After opening the app, closing the app, then re-opening, (maybe cycle that 1-3 times), Geolocator will hang for 16-20 seconds before finally getting a position.

Code sample

Code sample
  Future<Position> getCurrentLocation() async {
    const locationSettings = LocationSettings(
      accuracy: LocationAccuracy.medium, // Medium accuracy (100-500m)
    );
    final position = await Geolocator.getCurrentPosition(
      locationSettings: locationSettings,
    );
    return position;
  }

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

13.0.2

Flutter Doctor output

Doctor output
[√] Flutter (Channel beta, 3.32.0-0.1.pre, on Microsoft Windows [Version 10.0.22],
    locale en-US) [778ms]
    • Flutter version 3.32.0-0.1.pre on channel beta at C:\Flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eeb81b9a8a (4 days ago), 2025-04-11 12:11:52 -0700
    • Engine revision 72ee26e314
    • Dart version 3.8.0 (build 3.8.0-278.1.beta)
    • DevTools version 2.45.0

[√] Windows Version (11 Home 64-bit, 22H2, 2009) [4.9s]

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.6s]
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)
    • All Android licenses accepted.

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.13.6) [403ms]
    • Visual Studio Community 2022 version 17.13.35931.197
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2023.2) [25ms]
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)

[√] VS Code (version 1.99.2) [23ms]
    • Flutter extension version 3.108.0

[√] Network resources [458ms]
    • All expected network resources are available.

• No issues found!```

</details>
@TimHoogstrate
Copy link
Contributor

@corepuncher,

How did you test this, on a simulator? Or a real device?

Kind regards,

@TimHoogstrate TimHoogstrate self-assigned this Apr 16, 2025
@TimHoogstrate TimHoogstrate added the status: needs more info We need more information before we can continue work on this issue. label Apr 16, 2025
@corepuncher
Copy link
Author

corepuncher commented Apr 16, 2025

Real device. Galaxy S23+

At first I thought it was happening only when hot reloading during debugging.

However, when running the app alone on the device (debug OR release build), startup was also delayed after closing the app and immediately restarting.

@github-actions github-actions bot removed the status: needs more info We need more information before we can continue work on this issue. label Apr 16, 2025
@YarosMallorca
Copy link

Can confirm this is an issue. This doesn't happen if I set LocationAccuracy.high

@corepuncher
Copy link
Author

corepuncher commented May 6, 2025

I noticed that when the long hang/timeout occurs, there is no "location" symbol in the top status bar. Once the symbol finally appears, everything proceeds quickly. I wonder what it is "waiting" for?

Also, can confirm that "high" does not have the issue. But Low and Medium both do. "reduced" does NOT have the issue either.

"hang" times are typically 10-20 seconds.

@TimHoogstrate
Copy link
Contributor

I'am not sure about your testing conditions but can you post the LocationSettings that you use, maybe I can try to reproduce this? I cannot reproduce this on my own testing device. Also check if the device has Wifi or a simcard, because otherwise perhaps only the GPS is used, and this might take some time to warmup.

Kind regards,

@TimHoogstrate TimHoogstrate added the status: needs more info We need more information before we can continue work on this issue. label May 19, 2025
@YarosMallorca
Copy link

I do have wifi and sim card, it uses that (no location icon appears on the status bar).

@github-actions github-actions bot removed the status: needs more info We need more information before we can continue work on this issue. label May 19, 2025
@corepuncher
Copy link
Author

corepuncher commented May 20, 2025

@TimHoogstrate

  1. My S23 device has no sim card. Only wifi.

  2. My code is:

  Future<Position> getCurrentLocation() async {
    const locationSettings = LocationSettings(
      accuracy: LocationAccuracy.medium,
    );
    final position = await Geolocator.getCurrentPosition(
      locationSettings: locationSettings,
    );
    return position;
  }


Manifest:

   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  1. To reproduce:

A) open app, always works fast the first time.
B) Immediately close the app and re-open.
C) Hangs 15-20 seconds at the code above. If it does not hang on re-open, close and open again.
D) Even in release version, not a debugging issue (although I used debug to place print statements to see where hang is).
E) I use medium, but low also has the issue. High has no issue, but I'd rather not use high.

If you cannot re-produce, you could load up one of your geolocator files with 'got here 1' print statements, then send it to me and I'll replace and run it and figure out where it hangs at?

@corepuncher
Copy link
Author

@TimHoogstrate

Did some testing...

TEST A

  • Removed ACCESS_FINE_LOCATION from manifest completely.

  • Reinstalled app. The permission modal shows only the approximate location "map circle", and I choose allow while using.

  • No hanging using LocationAccuracy.medium. Interestingly, while my latitude is accurate to 3 decimals, my LONGITUDE is definitely "off" compared to reality by about .01. For example:

    45.36136136136136 <- interesting repeating pattern.....
    -100.49034140653207


TEST B

  • THEN, I put ACCESS_FINE_LOCATION back into manifest (along with COARSE).

  • Reinstalled app, and when permission modal shows, I left it on the first "map circle" which is fine location. I have never selected the approximate one on the right when having both as a choice.

  • Bug reproduced with LocationAccuracy.medium. First run, app is fast. Close, restart, hangs on geolocator. Lat lon printed:

    45.3611063
    -100.5008222

And then just for reference, although I will not be using high, the lat/lon printed is:

45.3610212
-100.5007815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants