flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

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

Open burekas7 opened this issue 1 year ago • 3 comments

Please check the following before submitting a new issue.

Please select affected platform(s)

  • [ ] Android
  • [X] 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!

burekas7 avatar Aug 23 '24 14:08 burekas7

+1

ItayBarFaye avatar Aug 26 '24 08:08 ItayBarFaye

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

ItayBarFaye avatar Aug 27 '24 07:08 ItayBarFaye

@ItayBarFaye thank you!

parasdaryanani avatar Sep 05 '24 17:09 parasdaryanani

Same the issue some on Android real devices =<<

gamestap99 avatar Nov 15 '24 03:11 gamestap99

Version 13.0.2, Android

There seems to be a much older version of this post from 2020: https://github.com/Baseflow/flutter-geolocator/issues/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;
  }

EatonWu avatar Dec 23 '24 23:12 EatonWu

@burekas7,

Did you test this with the supplied example app? Are you testing this indoor? Do you have a your wifi of cellular network available. Have you tried to omit any of the settings?

Kind regards,

TimHoogstrate avatar Jan 16 '25 11:01 TimHoogstrate

For me, @ItayBarFaye solution helps, since I've been using it I haven't had any problems so far.

burekas7 avatar Jan 18 '25 11:01 burekas7

@TimHoogstrate Usually the reports were inside the house, I don't know about the type of internet connection.

burekas7 avatar Jan 18 '25 11:01 burekas7

Is there any update on how to fix this issue?

adityandar avatar Feb 20 '25 07:02 adityandar

Inside the house the location could be hard to obtain. Try playing around with the settings like forcing the forceAndroidLocationManager to true or false, different accuracy settings or wifi, cellular on/off.

Kind regards,

TimHoogstrate avatar Mar 18 '25 08:03 TimHoogstrate

Without additional information, we are unfortunately not able to resolve this issue. Therefore, we reluctantly closed this issue for now. If you run into this issue later, feel free to file a new issue with a reference to this issue. Add a description of detailed steps to reproduce, expected and current behaviour, logs and the output of 'flutter doctor -v'. Thanks for your contribution.

github-actions[bot] avatar Apr 01 '25 08:04 github-actions[bot]

Without additional information, we are unfortunately not able to resolve this issue. Therefore, we reluctantly closed this issue for now. If you run into this issue later, feel free to file a new issue with a reference to this issue. Add a description of detailed steps to reproduce, expected and current behaviour, logs and the output of 'flutter doctor -v'. Thanks for your contribution.

github-actions[bot] avatar Apr 01 '25 08:04 github-actions[bot]