I'm new to flutter, I just want to ensure if the below code is correct, I want to check if the location permission was granted or no, if yes then get the current location and save into shared preferences and THEN go to the homepage route, otherwise go to the location page to ask the user for access his location
@override
void initState() {
super.initState();
checkLocation(context);
}
void checkLocation(context) async {
bool isGranted = await asyncFunction();
if(isGranted)
{
updateSettingLocation();
Navigator.of(context).pushNamed('/homepage');
} else{
Navigator.of(context).pushNamed('/location');
}
}
void updateSettingLocation() async{
final location = await currentLocation();
settingsRepo.setCurrentLocation(location);
}
Future<Position> currentLocation() {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
.then((location) {
if (location != null) {
print("Location: ${location.latitude},${location.longitude}");
}
return location;
});
}
void updateCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
settingsRepo.setCurrentLocation(position);
}
Future<bool> asyncFunction() async {
bool serviceEnabled;
LocationPermission permission;
permission = await Geolocator.checkPermission();
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (permission == LocationPermission.denied || !serviceEnabled || permission == LocationPermission.deniedForever) {
print('location access is denied');
return false;
} else {
print('location access is granted');
return true;
}
}