【发布时间】:2019-12-26 15:29:02
【问题描述】:
在 WeatherModel 下面的这段代码中,试图获取 android 手机的当前位置, 我的问题是一旦我开始运行它就会显示 NoSuchMethod Found,它说接收者为空, 因为我尝试了很多调试只是为了看看我的问题在哪里。 我现在明白我的问题是当我在 WeatherModel 中创建 Location() 实例时,经度和纬度为空,它永远不会获得价值,我不知道为什么......
对不起我的英语不好:(
const apiKey = 'e3653190f2b1d4803287b3074ecfe618';
const apiWeatherURL = 'https://api.openweathermap.org/data/2.5/weather';
class WeatherModel {
Future<dynamic> getLocationWeather() async {
Location location = Location();
NetworkHelper networkHelper = NetworkHelper(
'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey');
var weatherData = networkHelper.getData();
return weatherData;
}
}
.....
class Location {
double latitude;
double longitude;
Future<void> getCurrentLocation() async {
try {
Position _position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
longitude = _position.longitude;
print(longitude);
latitude = _position.latitude;
print(latitude);
} catch (e) {
print(e);
}
}
}
.........
class NetworkHelper {
NetworkHelper(this.url);
final url;
Future getData() async {
http.Response response = await http.get(url);
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
print(" Sarkawtua $data");
return data;
} else
print("Error ${response.statusCode} keshay Internet");
}
}
【问题讨论】: