snapshot.hasdata返回false,即使响应也有数据[flutter]
我正在尝试从OpenWeather获取当前的天气信息,并在检查返回数据后在文本小部件中显示结果。但是,始终将Condtion(Snapshot.hasdata)返回为false,否则调用条件(循环刺激器)。 这是FutureBuilder。
class WeatherPage extends StatefulWidget {
@override
State<WeatherPage> createState() => _WeatherPageState();
}
class _WeatherPageState extends State<WeatherPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: getCurrentWeather(),
builder: (context, snapshot) {
if (snapshot.hasData) {
WeatherModelCurrent weather =
snapshot.data as WeatherModelCurrent;
return weatherBox(weather);
} else {
return const CircularProgressIndicator();
}
},
),
),
);
}
这是getCurrentWeather()函数
Future getCurrentWeather() async {
WeatherModelCurrent? weather;
var url = Uri.parse(
"https://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=82c7b99e2a8215351147f607592a3e63&units=metric");
var response = await http.get(url);
//response.body has the data returned by API call
print(response.body);
if (response.statusCode == 200) {
weather = WeatherModelCurrent.frommJson(jsonDecode(response.body));
} else {
print('error');
}
return weather;
}
,这是模型类
class WeatherModelCurrent {
double temp;
double feelslike;
double tempmin;
double tempmax;
String description;
WeatherModelCurrent(
{required this.temp,
required this.feelslike,
required this.tempmin,
required this.tempmax,
required this.description});
factory WeatherModelCurrent.frommJson(Map<String, dynamic> jsonn) {
return WeatherModelCurrent(
temp: jsonn['main']['temp'].toDouble(),
feelslike: jsonn['main']['feels_like'].toDouble(),
tempmin: jsonn['main']['temp_min'].toDouble(),
tempmax: jsonn['main']['temp_max'].toDouble(),
description: jsonn['weather']['description'],
);
}
}
I am trying to get current weather info from OpenWeather and display the result in Text Widget after checking that there is data returned. However, that condtion (snapshot.hasData) is always returned as false and else condition (CircularProgressIndicator) is invoked.
Here is the FutureBuilder.
class WeatherPage extends StatefulWidget {
@override
State<WeatherPage> createState() => _WeatherPageState();
}
class _WeatherPageState extends State<WeatherPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: getCurrentWeather(),
builder: (context, snapshot) {
if (snapshot.hasData) {
WeatherModelCurrent weather =
snapshot.data as WeatherModelCurrent;
return weatherBox(weather);
} else {
return const CircularProgressIndicator();
}
},
),
),
);
}
here is the getCurrentWeather() function
Future getCurrentWeather() async {
WeatherModelCurrent? weather;
var url = Uri.parse(
"https://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=82c7b99e2a8215351147f607592a3e63&units=metric");
var response = await http.get(url);
//response.body has the data returned by API call
print(response.body);
if (response.statusCode == 200) {
weather = WeatherModelCurrent.frommJson(jsonDecode(response.body));
} else {
print('error');
}
return weather;
}
and here is the model class
class WeatherModelCurrent {
double temp;
double feelslike;
double tempmin;
double tempmax;
String description;
WeatherModelCurrent(
{required this.temp,
required this.feelslike,
required this.tempmin,
required this.tempmax,
required this.description});
factory WeatherModelCurrent.frommJson(Map<String, dynamic> jsonn) {
return WeatherModelCurrent(
temp: jsonn['main']['temp'].toDouble(),
feelslike: jsonn['main']['feels_like'].toDouble(),
tempmin: jsonn['main']['temp_min'].toDouble(),
tempmax: jsonn['main']['temp_max'].toDouble(),
description: jsonn['weather']['description'],
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试&amp;捕获您的
getCurrentWeather
代码块,我们可以获得以下例外:响应json类似:
您的代码
description:jsonn ['ateathy'] ['description'] ['description']
应该是描述:JSONN ['天气'] [0] ['Description'],
Try & catch your
getCurrentWeather
code block, we can get the following exception:The response json is like:
Your code
description: jsonn['weather']['description']
should bedescription: jsonn['weather'][0]['description'],