json - Flutter 在 json 数据中获取和循环
问题描述
由于我是flutter的新手,我按照教程构建了一个新闻应用程序,所以我制作了获取数据的类并且一切正常,当我执行应用程序时,我在获取数据方法中出现错误!有人能解释一下这段代码有什么问题吗!!
错误代码:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
类代码:
class FetchDataClass {
late String author;
late String title;
late String description;
late String url;
late String urlToImage;
late String publishedAt;
late String content;
FetchDataClass(this.author, this.title, this.description, this.url, this.urlToImage, this.publishedAt, this.content );
FetchDataClass.fromJson(Map<String, dynamic> jsonData) {
author = jsonData['author'];
title = jsonData['title'];
description = jsonData['description'];
url = jsonData['url'];
urlToImage = jsonData['urlToImage'];
publishedAt = jsonData['publishedAt'];
content = jsonData['content'];
}
}
抓取数据:
List<FetchDataClass> listofdata = List<FetchDataClass>.empty();
Future<List<FetchDataClass>> loadNews() async {
var response = await http.get(Uri.parse('https://newsapi.org/v2/everything?q=coronavirus&from=2021-09-10&sortBy=publishedAt&apiKey='));
List<FetchDataClass> news = List<FetchDataClass>.empty();
if(response.statusCode == 200) {
dynamic notesJson = json.decode(response.body);
for(dynamic noteJson in notesJson) { /// here the issue
print(11111);
news.add(FetchDataClass.fromJson(noteJson));
}
}
return news;
}
@override
void initState() {
loadNews().then((value) {setState(() {
listofdata.addAll(value);
});});
super.initState();
}
解决方案
如果您的 API 的所有数据都与模型匹配,您可以试试这个
class FetchDataClass {
String author;
String title;
String description;
String url;
String urlToImage;
String publishedAt;
String content;
FetchDataClass(
{required this.author,
required this.title,
required this.description,
required this.url,
required this.urlToImage,
required this.publishedAt,
required this.content});
factory FetchDataClass.fromJson(Map<String, dynamic> jsonData) {
return FetchDataClass(
author: jsonData['author'],
title: jsonData['title'],
description: jsonData['description'],
url: jsonData['url'],
urlToImage: jsonData['urlToImage'],
publishedAt: jsonData['publishedAt'],
content: jsonData['content'],
);
}
}
和获取数据服务
List<FetchDataClass> listofdata = List<FetchDataClass>.empty();
Future<List<FetchDataClass>> loadNews() async {
var response = await http.get(Uri.parse('https://newsapi.org/v2/everything?q=coronavirus&from=2021-09-10&sortBy=publishedAt&apiKey='));
List<FetchDataClass> news = List<FetchDataClass>.empty();
if(response.statusCode == 200) {
final notesJson = json.decode(response.body);
///final news = List<FetchDataClass>.from(
/// notesJson.map((model) => FetchDataClass.fromJson(model)));
final news = FetchDataClass.fromJson(model);
}
return news;
}
因为当您在模型类型中的 json 是 Map<String,dynamic> 但在获取数据时您设置了动态 notesJson,这是错误的类型。我为你修复了模型和服务调用 api,尝试一下,如果有问题你可以发短信给我帮助。
推荐阅读
- android - React Native npm run android 正在手机中安装旧的 apk 并寻找用于生成离线 apk 的自动化工具
- octobercms - 根据类别 1 设置唯一验证
- android - Android 无法使用签名的 url 将文件上传到谷歌云存储
- c - 关于猜词游戏制作高分表的问题
- .net-core - 如何将 net core 项目发布到 AnyCPU?
- php - 如何在我的 php 代码上添加路径目录,因为当我运行代码并上传文件时,我在第 29 行收到错误
- c++ - c++ 三元运算符期望表达式C/C++(29)
- python - NameError:在封闭范围内赋值之前引用了自由变量“mywidget”
- javascript - Javascript - 从两个数组创建嵌套的对象数组
- xml - 如何使用 grep 和 sed 修改 xml 配置文件?