#flutter #flutter-dependencies
Вопрос:
Я проверил разрешение на определение местоположения, включен или выключен GPS, и попытался узнать местоположение пользователя. На некоторых устройствах это работало для меня, но на других-нет. Как я могу устранить эту проблему в Flutter?
разрешение на проверку функций:
Future<void> _checkPerGranted() async {
try {
PermissionStatus perGranted = await location.hasPermission();
if (perGranted == PermissionStatus.denied) {
perGranted = await location.requestPermission();
_checkPerGrantedTwo(perGranted);
}else if (perGranted == PermissionStatus.deniedForever) {
perGranted = await location.requestPermission();
_checkPerGrantedTwo(perGranted);
}else{
_checklGpsEnable();
}
}catch(e){}
}
Он вызывается в том случае, если разрешения были даны после того, как они не были даны
Future<void> _checkPerGrantedTwo(var status) async {
try {
if (status == PermissionStatus.granted){
_checklGpsEnable();
}
}catch(e){}
}
проверьте, включен или выключен gps
Future<void> _checklGpsEnable() async {
try {
bool se = await location.serviceEnabled()??false;
bool se2 = await Geolocator.isLocationServiceEnabled()??false;
if (se) {
if (await location.isBackgroundModeEnabled() != true)
location.enableBackgroundMode(enable: true);
LocationData lo = await location.getLocation();
_center = new LatLng(lo.latitude, lo.longitude);
notifyListeners();
} else if (se2) {
position = await Geolocator.getCurrentPosition();
_center = LatLng(position.latitude, position.longitude);
if (await location.isBackgroundModeEnabled() != true)
location.enableBackgroundMode(enable: true);
notifyListeners();
} else {
_center = LatLng(18.079021, -15.965662);
se = await location.requestService();
notifyListeners();
if (se) {
LocationData lo = await location.getLocation();
_center = LatLng(lo.latitude, lo.longitude);
location.enableBackgroundMode(enable: true);
notifyListeners();
}
}
}on Error catch (e) {
// showAlertDialogCancelByDriver(Context,"catch" e.toString());
_center = new LatLng(18.079021, -15.965662);
notifyListeners();
}
}