COD 400,消息:错误的纬度,开放服:获取当前的WeatherData
当我试图获取当前天气数据时,我会遇到错误(纬度错误)。谁能告诉我出了什么问题 我尝试将Location Accuracy从低点更改为最佳,但并没有改变任何
Location location = Location();
class _LoadingScreenState extends State<LoadingScreen> {
String weatherABI = '4ead5f41d1c622302dd34242f9d1c25a';
void getlocation() async {
await location.getCurrentLocation();
print(location.latitude);
print(location.longtiude);
}
void getData() async {
Uri url = Uri.parse(
'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtiude}&appid=$weatherABI');
Response response = await get(url);
print(response.body);
if (response.statusCode == 200) {
String data = response.body;
print(data);
} else {
print(response.statusCode);
}
}
@override
void initState() {
super.initState();
getlocation();
getData();
}
Widget build(BuildContext context) {
return Scaffold();
}
}
I'm getting error (wrong latitude) when I'm trying to get current weather data. can anyone tell me what went wrong
I tried changing locationAccuracy from low to best but it didn't change anything
Location location = Location();
class _LoadingScreenState extends State<LoadingScreen> {
String weatherABI = '4ead5f41d1c622302dd34242f9d1c25a';
void getlocation() async {
await location.getCurrentLocation();
print(location.latitude);
print(location.longtiude);
}
void getData() async {
Uri url = Uri.parse(
'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtiude}&appid=$weatherABI');
Response response = await get(url);
print(response.body);
if (response.statusCode == 200) {
String data = response.body;
print(data);
} else {
print(response.statusCode);
}
}
@override
void initState() {
super.initState();
getlocation();
getData();
}
Widget build(BuildContext context) {
return Scaffold();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 getLocation()函数末尾调用您的 getData()函数,因为两个函数同时调用,这就是为什么 getData()函数的原因没有获得任何位置数据。您需要先获取位置,然后需要调用 getData 功能。
删除 getData()函数从 initstate()
您还需要在 getData()函数中调用SetState(一旦从API检索数据)以更新您的UI。
Just call your getData() function in the end of getLocation() function because both functions called at same time that's why getData() function does not get any location data. You need to get location first then you need to call getData function.
Remove getData() function from initState()
You also need to call setState in getData() function once you retrieved data from the API to update your UI.