当我试图获取当前的天气数据时,我得到了错误(错误的纬度)。有人能告诉我出了什么问题吗?我试着把locationAccuracy从低变到最好,但它没有改变任何东西
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();
}
}错误:

发布于 2022-04-14 14:59:46
只需在getData()函数的末尾调用函数,因为这两个函数同时调用,这就是getData()函数没有获得任何位置数据的原因。您需要先获得位置,然后调用getData函数。
将getData()函数从initState()中删除
void getlocation() async {
await location.getCurrentLocation();
getData();
print(location.latitude);
print(location.longtiude);
}您还需要在从API中检索数据以更新UI时,调用getData()函数中的。
https://stackoverflow.com/questions/71871570
复制相似问题