颤振JSON序列化错误

颤振JSON序列化错误,json,dart,flutter,Json,Dart,Flutter,我想从传入的API请求将JSON对象序列化为本机类对象。但是,当映射发生并且FutureBuilder尝试返回数据时,它会生成一个运行时错误,如下所示: new FutureBuilder<BaseClass>( future: getWeatherInfo(...), builder: (BuildContext context, AsyncSnapshot<BaseClass> snapshot) { .... 类型\u CastError不是bool的子类

我想从传入的API请求将JSON对象序列化为本机类对象。但是,当映射发生并且
FutureBuilder
尝试返回数据时,它会生成一个运行时错误,如下所示:

new FutureBuilder<BaseClass>(
  future: getWeatherInfo(...),
  builder: (BuildContext context, AsyncSnapshot<BaseClass> snapshot) {
....
类型\u CastError不是bool的子类型

使用列表返回数据和

类型_TypeError不是bool的子类型

根据这些错误,我假设它无法从流中强制转换数据。我在学习颤振时没有太多的想法

以下是我提出请求的未来:

Future getWeatherInfo(String city) async {
Uri uri = new Uri.https("api.apixu.com", "/v1/current.json",
  {"key": <key>, "q": city});
print(uri);
final response = await http.get(uri);
final responseJSON = jsonDecode(response.body);
print(responseJSON);
return BaseClass.getData(responseJSON);
列表视图:

class BaseClass {
// final WeatherInformation weatherInformation;
final Weather weather;
BaseClass({this.weather});
factory BaseClass.getData(Map jsonData) {
return new BaseClass(
  // weatherInformation: (jsonData["location"])
  //     .map((i) => WeatherInformation.weatherValues(i)),
  weather: (jsonData["current"])
      .map((i) => Weather.weatherData(i)),
);
}}


class WeatherInformation {
final String cityName;
final String cityRegion;
final String country;
final double lat;
final double long;
final String time;

WeatherInformation({
this.cityName,
this.cityRegion,
this.country,
this.time,
this.lat,
this.long,
});

factory WeatherInformation.weatherValues(Map data) {
  return new WeatherInformation(
  cityRegion: data['cityRegion'],
  cityName: data['name'],
  country: data['country'],
  lat: data['lat'],
  long: data['long'],
  time: data['localtime'],
);
}
}

class Weather {
final Condition condition;

final double windmph;
final double windkph;
final double winddegree;
final int cloud;
final double pressuremb;
final double pressurein;
final double precipmm;
final double precipin;
final int humidity;
final double centrigade;
final double faranheit;
final double tempc;
final double tempf;

Weather({
this.centrigade,
this.cloud,
this.faranheit,
this.humidity,
this.pressuremb,
this.tempc,
this.precipmm,
this.precipin,
this.pressurein,
this.tempf,
this.winddegree,
this.windkph,
this.windmph,
this.condition,
});

factory Weather.weatherData(Map data) {
return new Weather(
  tempc: data['temp_c'],
  tempf: data['temp_f'],
  windmph: data['wind_mph'],
  windkph: data['wind_kph'],
  winddegree: data['wind_degree'],
  pressuremb: data['pressure_mb'],
  pressurein: data['pressure_in'],
  precipmm: data['precip_mm'],
  precipin: data['precip_in'],
  humidity: data['humidity'],
  cloud: data['cloud'],
  centrigade: data['feelslike_c'],
  faranheit: data['feelslike_f'],
  // condition:
  //     (data['condition'] as List).map((i) => Condition.toJSON(i)).toList(),
);
}
}

class Condition {
String text;
String icon;

Condition({
Key key,
this.text,
this.icon,
});

Condition.toJSON(Map data)
  : icon = data['icon'],
    text = data['text'];
}
** if (snapshot.hasError)** { //(previously was snapshot.error)
return new Center(
child: new Chip(
label: new Text('Error! Please try again',style: new TextStyle(color: 
Colors.white),)));
} else if (snapshot.hasData) {
return new ListView(
shrinkWrap:false,
children: <Widget>[
new ListTile(
leading: new Text("Calvin"),
                      title: new 
Text(snapshot.data.weather[0].tempc.toString(),
style: new TextStyle(color: Colors.white)),),
new ListTile(
leading: new Text('Pressure'),
title: new Text(
snapshot.data.weather[0].pressurein.toString(),
style: new TextStyle(color: Colors.white)),
 ),
new ListTile(
title: new Text(snapshot.data.weather[0].cloud.toString()),
leading: new Icon(Icons.cloud)),
 ],
 );
 }
**if(snapshot.hasError)**{//(以前是snapshot.error)
返回新中心(
孩子:新芯片(
标签:新文本('错误!请重试',样式:新文本样式(颜色:
颜色:白色);
}else if(snapshot.hasData){
返回新的ListView(
收缩膜:假,
儿童:[
新ListTile(
导语:新文本(“加尔文”),
标题:新
文本(snapshot.data.weather[0].tempc.toString(),
样式:新文本样式(颜色:Colors.white)),),
新ListTile(
前导:新文本(“压力”),
标题:新文本(
snapshot.data.weather[0].pressurein.toString(),
样式:新文本样式(颜色:Colors.white)),
),
新ListTile(
标题:新文本(snapshot.data.weather[0].cloud.toString()),
领先:新图标(Icons.cloud)),
],
);
}

将来没有类型。因此,
异步快照
可能无法正常工作

在上面的第一个列表中使用
Future

然后,在
FutureBuilder
中添加它,如下所示:

new FutureBuilder<BaseClass>(
  future: getWeatherInfo(...),
  builder: (BuildContext context, AsyncSnapshot<BaseClass> snapshot) {
....
new FutureBuilder(
未来:getWeatherInfo(…),
生成器:(BuildContext上下文,异步快照){
....

您在代码中的何处得到错误?您是否在某处有
if
语句?是的,在构建ListView时,我正在检查快照是否有错误。能否将该代码添加到您的问题中?我正在使用返回对象的error属性:[我希望他们只记录出错的字段,避免让我们陷入猜测游戏中。事实上,以前我已将future的类型设置为BaseClass,但无法访问列表和类属性,因此默认情况下,我将其保留为dynamicI,但出现了一个异常,该异常表示_InternalLinkedHashmap不是类型List的子类型,您应该尝试解决此问题错误。当映射需要列表时,您正在传递映射。请尝试查找发生这种情况的行。工厂中的
jsonData[“当前”]
是列表还是映射?如果您在映射上调用
.Map()
方法,则它应该是列表。