BTW- Things work as anticipated on IOS. The trouble I'm having is running on Android (Nexus 5 API 30)
I have carefully followed the instructions for setup paying special attention to the gradle.properties and "android/app/build.gradle" The AndroidManifest.xml was also updated to include the
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION
uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION
when the app starts, there is prompting asking to allow location services.
I can even check that the location services on the app are indeed enabled-
Here is the code that calls geolocation...
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void initState() {
super.initState();
getLocation();
}
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
When I run the code there is a very long list of exceptions the first few are:
E/flutter (21698): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: The location service on the device is disabled.
E/flutter (21698): #0 MethodChannelGeolocator._handlePlatformException (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:192:9)
E/flutter (21698): #1 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:121:7)
E/flutter (21698): <asynchronous suspension>
E/flutter (21698): #2 Geolocator.getCurrentPosition (package:geolocator/geolocator.dart:258:35)
try{
await getLocation();
} catch(e,s){
//handle errors
}
and some of the Android emulators has this exact problem (bug), try a different emulator, or device
initialize variable
Position _currentPosition;
code
_getCurrentLocation() async{
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
print(_currentPosition);
});
}).catchError((e) {
print(e);
});
}
Run flutter clean
uninstall the app from phone/emulator and restart the phone/emulator then run the app again after accepting permission it will ask for enable location dialog automatically,if the problem occurs try with location package
Adding forceAndroidLocationManager:true made it work for me.
position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium,
forceAndroidLocationManager: true)
.timeout(Duration(seconds: 10));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.