Skip to content
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

[Bug]: Geolocator.getCurrentPosition() is not stable #1563

Open
3 of 8 tasks
burekas7 opened this issue Aug 23, 2024 · 5 comments
Open
3 of 8 tasks

[Bug]: Geolocator.getCurrentPosition() is not stable #1563

burekas7 opened this issue Aug 23, 2024 · 5 comments

Comments

@burekas7
Copy link

burekas7 commented Aug 23, 2024

Please check the following before submitting a new issue.

Please select affected platform(s)

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

Steps to reproduce

Hi,

I'm running my appp on iOS 17.5.1 (real device)
(Precise Location in settings is ON, and all the relevant permissions in info.plist are there)

Using geolocator version 12.0.0

When try to get a location using "Geolocator.getCurrentPosition()"
(desiredAccuracy: LocationAccuracy.high but also with "best")

Somtimes it takes few seconds, sometimes few minutes, and sometimes it seems like it never ends.

What could be the reason and how can I solve this?
Is there any limitation in iOS or something?
(It was ok in the past)

(It happens also with "location' package, but there is it worst, it never completes)

Thanks

Expected results

A desired result that will end after a maximum of a few seconds.

Actual results

Get a resopnse agter few secongs maximum.

Code sample

Code sample
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

12.0.0

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.6.1 23G93 darwin-arm64, locale en-IL)
• Flutter version 3.19.5 on channel stable at /Users/adi/Downloads/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 300451adae (5 months ago), 2024-03-27 21:54:07 -0500
• Engine revision e76c956498
• Dart version 3.3.3
• DevTools version 2.31.1
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/adi/Library/Android/sdk
• Platform android-34-ext8, build-tools 34.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15A507
• CocoaPods version 1.15.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.2)
• Android Studio at /Applications/Android Studio.app/Contents
• 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-17.0.9b1087.7-11185874)
[✓] VS Code (version 1.92.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.94.0
[✓] Connected device (3 available)
• Adi’s iPhone (mobile) • 00008020-001059A91EE9002E • ios • iOS 17.5.1 21F90
• macOS (desktop) • macos • darwin-arm64 • macOS 14.6.1 23G93 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 127.0.6533.122
[✓] Network resources
• All expected network resources are available.
• No issues found!
@ItayBarFaye
Copy link

+1

@ItayBarFaye
Copy link

@burekas7, I think I found something to make it works. Try this -
unawaited(Geolocator.getCurrentPosition());
await Geolocator.getPositionStream().first;

@parasdaryanani
Copy link

@ItayBarFaye thank you!

@gamestap99
Copy link

Same the issue some on Android real devices =<<

@EatonWu
Copy link

EatonWu commented Dec 23, 2024

Version 13.0.2, Android

There seems to be a much older version of this post from 2020:
#559

I'd like to add some data; this problem occurs for our project, and I've collected some data on how often it happens. I am using @ItayBarFaye's fix and the bug continues to appear. It seems to be intermittent, yet consistent.

image

I am simply restarting my app with a 6 second timer. If the getPositionStream/getCurrentPosition succeeds by then, it logs a success. if it does not, it logs a failure. I can't seem to determine a cause; it's certainly not network connectivity.

I've chosen to mitigate this by simply using the cached version and updating the position with an unawaited call to the getPositionStream:

  Future<LatLng?> tryGetLocation() async {
    unawaited(Geolocator.getCurrentPosition());
    position = await Geolocator.getLastKnownPosition();
    if (position != null) {
      Geolocator.getPositionStream().first.then((value) {
        position = value;
      });
      return location;
    }
    else {
      position = await Geolocator.getPositionStream().first;
    }
    return location;
  }

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

5 participants