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

[Question]: Geolocator's Location Stream does not seems to be working sometimes.

Open HeronKyaw opened this issue 1 year ago • 7 comments

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

  • [X] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] macOS
  • [ ] Web
  • [ ] Windows

Your question

When I get into the map page without enabling the location service. Everything works as expected. However, when I turn on location again, the location stream does not do anything inside of it (such as .listen and .handleError methods). All the logic and prints inside those mentioned method does not work. I didn't set location settings for specific platforms, so Android should be working with FusedLocationProvider, right?

Anyway, I tried using AndroidSettings with forceLocationManager: true, the stream is working as expected but I don't wanna use legacy location manager for this.

Here's my code

LocationSettings locationSettings = const LocationSettings(
  accuracy: LocationAccuracy.high,
  distanceFilter: 100,
);

void positionListening() {
  if (_positionStreamSubscription == null) {
    final Stream<Position> positionStream = geolocatorPlatform.getPositionStream(
        locationSettings: locationSettings);
    _positionStreamSubscription = positionStream.handleError((error) async {
      if (kDebugMode) {
        print('position stream error $error');
      }
      _positionStreamSubscription?.cancel();
      _positionStreamSubscription = null;
    }).listen((position) {
      if (kDebugMode) {
        print('current position: ${position.latitude} ${position.longitude}');
      }
      updateLocation(position.latitude, position.longitude);
      if (isGoogleMap == 0) {
        moveToRegion(position.latitude, position.longitude);
      }
    });
  } else {
    updateLocation(latitude, longitude);
  }
}
      

Version

^9.0.2

HeronKyaw avatar Dec 04 '23 05:12 HeronKyaw

Dear @HeronKyaw,

Did you request the correct permissions? And can you explain a bit how you are testing this? Are you testing inside or outside, with a real device or an emulator. And can you paste the output of flutter doctor -v and your manifest file into this post?

Kind regards,

TimHoogstrate avatar Dec 05 '23 13:12 TimHoogstrate

@TimHoogstrate, yup, I did request correct permission, and I'm testing both inside and outside with real device.

Here's the permission inside AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Flutter Doctor:

[10:37:24] [~] ❱❱❱ flutter doctor -v
[✓] Flutter (Channel stable, 3.10.6, on macOS 14.1.2 23B92 darwin-arm64, locale
    en-MM)
    • Flutter version 3.10.6 on channel stable at /Users/gwt/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (5 months ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/gwt/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment Homebrew (build 17.0.7+0)
    • 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.13.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version 2023.1)
    • 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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (3 available)
    • H3R0n (mobile)  • 166b4ec7dfd99876b662a7185371e4a525309a5e • ios
      • iOS 16.7.2 20H115
    • macOS (desktop) • macos                                    • darwin-arm64
      • macOS 14.1.2 23B92 darwin-arm64
    • Chrome (web)    • chrome                                   •
      web-javascript • Google Chrome 119.0.6045.199

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 categories.

Anyway, I have found a walkaway for this. I have to call something related to getting location such as getCurrentLocation() first before calling the stream. This way works but I think the stream should be working alone, right?

Test case:

  1. Go to map page which calls the stream without opening GPS.
  2. Then once inside of it, try opening the GPS.
  3. Here we don't get any response from the stream.

NOTE: With debug, you'll not face the issue. Try logging this out.

HeronKyaw avatar Dec 08 '23 04:12 HeronKyaw

Thank you for providing the additional information. I have labelled this issue as a bug and added it to our backlog.

mvanbeusekom avatar Jan 15 '24 10:01 mvanbeusekom

Any updates on this?

AEZakmee avatar Mar 15 '24 10:03 AEZakmee

Any updates for this bug, @mvanbeusekom?

HeronKyaw avatar Apr 25 '24 10:04 HeronKyaw

Any updates for this bug, @mvanbeusekom ???

shams098 avatar Jun 20 '24 15:06 shams098