在 flutter 中存储 LocationData 的问题
Problem in storing LocationData in flutter
我正在尝试获取用户的位置坐标并将其存储在变量中。
我用的是flutter的位置库
但是我得到这个错误:
type 'Future' is not a subtype of type 'Future'
这是我的代码
Future<LocationData> coords;
double lat, long;
coords = getUserLocationCoordinates();
coords.then((value) => {
lat = value.latitude,
long = value.longitude,
});
我的 getUserLocationCoordinates 函数
getUserLocationCoordinates() async {
LocationData currentLocation;
String error;
Location location = Location();
try {
currentLocation = await location.getLocation();
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'please grant permission';
print(error);
}
if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error = 'permission denied- please enable it from app settings';
print(error);
}
currentLocation = null;
}
return currentLocation;
}
非常感谢任何帮助
初始化变量
String latitude_data;
String longitude_data;
bool _serviceEnabled;
当前位置函数
Future _getLocation() async {
Location location = new Location();
var _permissionGranted = await location.hasPermission();
_serviceEnabled = await location.serviceEnabled();
if (_permissionGranted != PermissionStatus.granted || !_serviceEnabled) {
_permissionGranted = await location.requestPermission();
_serviceEnabled = await location.requestService();
} else {
///Do something here
}
LocationData _currentPosition = await location.getLocation();
longitude_data=_currentPosition.longitude.toString();
latitude_data=_currentPosition.latitude.toString();
///if you want you can save data to sharedPrefrence
SharedPrefrence().setLatitude(_currentPosition.latitude.toString());
SharedPrefrence().setLongitude(_currentPosition.longitude.toString());
}
我正在尝试获取用户的位置坐标并将其存储在变量中。
我用的是flutter的位置库
但是我得到这个错误:
type 'Future' is not a subtype of type 'Future'
这是我的代码
Future<LocationData> coords;
double lat, long;
coords = getUserLocationCoordinates();
coords.then((value) => {
lat = value.latitude,
long = value.longitude,
});
我的 getUserLocationCoordinates 函数
getUserLocationCoordinates() async {
LocationData currentLocation;
String error;
Location location = Location();
try {
currentLocation = await location.getLocation();
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'please grant permission';
print(error);
}
if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error = 'permission denied- please enable it from app settings';
print(error);
}
currentLocation = null;
}
return currentLocation;
}
非常感谢任何帮助
初始化变量
String latitude_data;
String longitude_data;
bool _serviceEnabled;
当前位置函数
Future _getLocation() async {
Location location = new Location();
var _permissionGranted = await location.hasPermission();
_serviceEnabled = await location.serviceEnabled();
if (_permissionGranted != PermissionStatus.granted || !_serviceEnabled) {
_permissionGranted = await location.requestPermission();
_serviceEnabled = await location.requestService();
} else {
///Do something here
}
LocationData _currentPosition = await location.getLocation();
longitude_data=_currentPosition.longitude.toString();
latitude_data=_currentPosition.latitude.toString();
///if you want you can save data to sharedPrefrence
SharedPrefrence().setLatitude(_currentPosition.latitude.toString());
SharedPrefrence().setLongitude(_currentPosition.longitude.toString());
}