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

getCurrentPosition() never returns

Open ablbol opened this issue 2 years ago • 9 comments

🐛 Bug Report

When I make the following call, the getCurrentPosition() function never return and the app keeps waiting for ever.

Geolocator.getCurrentPosition(
  desiredAccuracy: LocationAccuracy.lowest,
  forceAndroidLocationManager: true, // I also did not use this property.
);

Expected behavior

I expect the getCurrentPosition() function to return back the position but it never returns.

Reproduction steps

The app makes sure that it has all permissions and then calls the function. I have noticed this behaviour since I moved to geolocator 8.2.0 couple weeks ago. I never had this problem before.

Configuration

In my AndroidManifest.xml, I have the following permissions:

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

Version: geolocator: ^8.2.0

Platform: I am using Huawei LYA-L29 with Android v 10 as well as LG-G4 with Android v 6.

ablbol avatar Mar 26 '22 13:03 ablbol

~7.0.1~ 7.5.0 seems to be working for me. I did a force downgrade to get this working while this gets fixed. It was causing issues on Android and iOS

droidpl avatar Mar 28 '22 19:03 droidpl

7.0.1 doesn’t seem to be available any longer. The current in the 7 series seems to be 7.7.1 and it’s broken too.

mauricev avatar Apr 11 '22 05:04 mauricev

This is looking a like an issue with iOS simulator, at least in my case. It doesn’t appear to support location services although it apparently grants permissions for it.

mauricev avatar Apr 12 '22 04:04 mauricev

@mauricev The iOS simulator by default has location None. If you set a value it should work. At least, it does in my case.

funkyboy avatar Apr 12 '22 10:04 funkyboy

Where would I do that? There is no Location Services in the simulated iPhone 8.

mauricev avatar Apr 13 '22 02:04 mauricev

@mauricev See https://stackoverflow.com/a/71005159

funkyboy avatar Apr 13 '22 08:04 funkyboy

A-ha. Thank you. It works now.

mauricev avatar Apr 15 '22 04:04 mauricev

Same for me. The function just calls but never returns anything.. Tested on current flutter version, current lib version and Android 11 Hardware Device

vicdotdevelop avatar May 04 '22 16:05 vicdotdevelop

ACCESS_FINE_LOCATION Permission Is Required. I Have this issue

bahiraei avatar Jun 29 '22 08:06 bahiraei

I have both of those in my Android Manifest and it still doesn't work @bahiraei

DanMossa avatar Sep 24 '22 02:09 DanMossa

~7.0.1~ 7.5.0 seems to be working for me. I did a force downgrade to get this working while this gets fixed. It was causing issues on Android and iOS

Downgrading at 7.5.0 as @droidpl said worked for me

MrAlessandro avatar Sep 30 '22 07:09 MrAlessandro

Is this problem still relevant? We have made a lot of improvements since the time this issue was created and I am unable to reproduce this issue on our test devices (Google Pixel 7A and Google Nexus 5X).

To reproduce the error I created a new Flutter application and added simple logic to get the current location on button press. Here are more detailed steps:

  1. Create a new flutter application: flutter create geo_issue_1015.
  2. Change into the root of the new application: cd geo_issue_1015.
  3. Add dependency on the geolocator plugin: flutter pub add geolocator.
  4. Add the required permissions to the android/app/src/main/AndroidManifest.xml file (see code below).
  5. Add the required logic to get the current position to lib/main.dart (see code below).
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:label="geo_issue_1015"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
main.dart
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _position = '';

  Future<void> _acquiredPosition() async {
    await Geolocator.requestPermission();

    final Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.lowest,
      forceAndroidLocationManager: true,
    );

    setState(() {
      _position = position.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Acquired position:',
            ),
            Text(
              _position,
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _acquiredPosition,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

After adding this code, I run the application, click the + button, allow location permission and the location is returned. Note the on the Google Nexus 5X device it took a few seconds to acquire the position which is normal.

It would be very helpful if someone can confirm this is still an issue and provide some additional information:

  1. Output of the flutter doctor -v command.
  2. Example application reproducing the issue.
  3. Details on devices used to test with.

mvanbeusekom avatar Aug 17 '23 09:08 mvanbeusekom

We have also faced this issue in Android Device (Pixel 5) Emulator running on API level 22.

musaffa avatar Aug 21 '23 15:08 musaffa

Dear @musaffa,

Could you please elaborate a bit on your issue or create a new issue. It could be that your issue is related to your dev environment, because you are running on an emulator. Also don't forget to add the output of flutter doctor -v to this issue.

Kind regards,

TimHoogstrate avatar Aug 28 '23 08:08 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 Sep 11 '23 09:09 github-actions[bot]