【发布时间】:2022-12-31 02:14:35
【问题描述】:
我正在尝试通过学习 Angela Yu 课程来学习 Flutter,这似乎有点过时了,我正面临这个问题:“不要在异步间隙中使用 BuildContexts”,在以下代码中;
void getLocationData() async {
Location location = Location();
await location.getCurrentLocation();
NetworkHelper networkHelper = NetworkHelper(
'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey&units=metric');
var weatherData = await networkHelper.getData();
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(locationWeather: weatherData);
}));
}
我试图在网上查看解决方案,但似乎找不到我的查询的答案。任何反馈将不胜感激。
附言我尝试使用:
if (mounted) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(locationWeather: weatherData);
}));
}
但它似乎也不起作用。
【问题讨论】: