【发布时间】:2021-11-29 15:00:02
【问题描述】:
由于我是 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();
}
【问题讨论】:
-
请包含您遇到的错误。它可能表明确切的问题是什么,并且通常它甚至会直接告诉您如何解决它:)
-
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' -
@Boaz 我更新问题
-
@Ihab07 您应该包含 HTTP 请求返回的数据。作为错误,它可能喜欢
{a:b,c:d}(这是一张地图)而不是[{a:b,c:d},{e:f,g:h}](这是你想要的列表)。你也可以看看this。