flutter-geolocator
flutter-geolocator copied to clipboard
getCurrentPosition() never returns
🐛 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.
~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
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.
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 The iOS simulator by default has location None
. If you set a value it should work. At least, it does in my case.
Where would I do that? There is no Location Services in the simulated iPhone 8.
@mauricev See https://stackoverflow.com/a/71005159
A-ha. Thank you. It works now.
Same for me. The function just calls but never returns anything.. Tested on current flutter version, current lib version and Android 11 Hardware Device
ACCESS_FINE_LOCATION Permission Is Required. I Have this issue
I have both of those in my Android Manifest and it still doesn't work @bahiraei
~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
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:
- Create a new flutter application:
flutter create geo_issue_1015
. - Change into the root of the new application:
cd geo_issue_1015
. - Add dependency on the geolocator plugin:
flutter pub add geolocator
. - Add the required permissions to the
android/app/src/main/AndroidManifest.xml
file (see code below). - 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:
- Output of the
flutter doctor -v
command. - Example application reproducing the issue.
- Details on devices used to test with.
We have also faced this issue in Android Device (Pixel 5) Emulator running on API level 22.
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,
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.