gpt4 book ai didi

json - _InternalLinkedHashMap 类型不是 List 类型的子类型

转载 作者:IT王子 更新时间:2023-10-29 06:54:57 28 4
gpt4 key购买 nike

我正在尝试使用网络调用实现一个简单的新闻提要应用程序,其中屏幕将在 ListView 中显示最新的故事。

使用当前代码,我从 API 获得了响应(因为我在日志中看到了整个响应主体),但似乎无法在 UI 中显示数据。我收到此错误:

type _InternalLinkedHashMap<String, dynamic> is not subtype of type List

这是JSON结构

{
"response": {
"status": "ok",
"userTier": "developer",
"total": 25095,
"startIndex": 1,
"pageSize": 10,
"currentPage": 1,
"pages": 2510,
"orderBy": "relevance",
"results": [
{
"id": "australia-news/2018/aug/13/turnbulls-energy-policy-hangs-in-the-balance-as-euthanasia-debate-given-precedence",
"type": "article",
"sectionId": "australia-news",
"sectionName": "Australia news",
"webPublicationDate": "2018-08-12T18:00:08Z",
"webTitle": "Energy policy hangs in balance, as Senate debates euthanasia",
"webUrl": "https://www.theguardian.com/australia-news/2018/aug/13/turnbulls-energy-policy-hangs-in-the-balance-as-euthanasia-debate-given-precedence",
"apiUrl": "https://content.guardianapis.com/australia-news/2018/aug/13/turnbulls-energy-policy-hangs-in-the-balance-as-euthanasia-debate-given-precedence",
"isHosted": false,
"pillarId": "pillar/news",
"pillarName": "News"
}, {
"id": "media/2018/jun/13/the-rev-colin-morris-obituary-letter",
"type": "article",
"sectionId": "media",

根据我的理解,我只想在列表中先显示webTitle,然后再添加其他字段(在我清楚网络概念之后),但出现上述错误。这是我的完整代码:

class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {

return new MaterialApp(
title: 'Network Example',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
appBar: AppBar(
title: new Text('Network Example'),
),
body: new Container(
child: new FutureBuilder<List<News>> (
future: fetchNews(),
builder: (context, snapshot) {

if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(snapshot.data[index].newsTitle,
style: new TextStyle(
fontWeight: FontWeight.bold)
),
new Divider()
],
);
}
);
} else if (snapshot.hasError) {
return new Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
),
),
);
}


Future<List<News>> fetchNews() async {
final response = await http.get('https://content.guardianapis.com/search?q=debates&api-key=');
print(response.body);
List responseJson = json.decode(response.body.toString());
List<News> newsTitle = createNewsList(responseJson);
return newsTitle;

}

List<News> createNewsList(List data) {
List<News> list = new List();
for (int i = 0; i< data.length; i++) {
String title = data[i]['webTitle'];

News news = new News(
newsTitle: title);
list.add(news);
}
return list;

}
}

class News {
final String newsTitle;

News({this.newsTitle});

factory News.fromJson(Map<String, dynamic> json) {

return new News(
newsTitle: json['webTitle'],
);
}
}

我之前看过类似的问题,也看过 json 结构的文章,但似乎无法弄清楚如何解决这个问题。

最佳答案

问题是,您的 JSON 不是数组。它是一个对象。但是您尝试将其用作数组。

您可能希望将 createNewsList 调用更改为以下内容:

List responseJson = json.decode(response.body.toString());
List<News> newsTitle = createNewsList(responseJson["response"]["results"]);

关于json - _InternalLinkedHashMap<String, dynamic> 类型不是 List<dynamic> 类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937609/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com