I am always getting that message when trying to get my current location. I am running my app on real device. I followed the documentation instructions here geolocator plugin. At first it gets the location, then after quitting the app ang start it again,it says that
The location service is on the device is disabled
Why I am getting that error even though I already enabled the location services?
class LocationController extends GetxController{
var position = Position().obs;
var lat = 0.00.obs;
var long = 0.00.obs;
Future getLocation() async {
try{
position.value = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
lat.value = position.value.latitude;
long.value = position.value.longitude;
} catch (e){
print(e);
}
}
}
Call...
locationController.getLocation();
Download & Import Plugin
geolocator and geocoding
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
In your AndroidManifest add permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
initialize variable
Position _currentPosition;
String _currentAddress = "";
Current Location Function
_getCurrentLocation() async {
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
//Pass the lat and long to the function
_getAddressFromLatLng(position.latitude, position.longitude);
});
}).catchError((e) {
print(e);
});
}
_getAddressFromLatLng
_getAddressFromLatLng(double latitude, double longitude) async {
try {
List<Placemark> p = await placemarkFromCoordinates(latitude, longitude);
Placemark place = p[0];
setState(() {
_currentAddress = "${place.locality}";
print(_currentAddress);
});
} catch (e) {
print(e);
}
}
If above problem occurs just run flutter clean
,uninstall the app then restart the phone and run the project again