flutter - 如何在 Flutter 的 Listview 中发布不同类型的 JSON 对象
问题描述
我正在尝试在我的颤振应用程序中实现类似新闻源的东西。下面是我从后端收到的 Json
[
{
"date": "2020-03-25T12:29:12.913Z",
"comments": [
"5e7b52ce5c28ee49f8e601a6",
"5e7b530fdcfdf940cc4862c3",
"5e7b553e0d2a54433cbbf46b"
],
"UpVote": [
"5e79a7efc8a01207cc3f8b1d"
],
"DownVote": [],
"_id": "5e7b4e98ccc6d52a4467d096",
"Author": "5e79a7efc8a01207cc3f8b1d",
"__v": 0
},
{
"_id": "5e7e448bc27b4206280754d5",
"NewsSource": "The Washington Post",
"Reporter": "Carolyn Y. Johnson, Ben Guarino",
"Title": "Blood from people who recover from coronavirus could provide a treatment - The Washington Post",
"Description": "People who recover from the coronavirus could play an essential role in fighting the disease -- by donating plasma that could help others fight off infection.",
"Content": "The possible therapy is based on a medical concept called passive immunity. People who recover from an infection develop antibodies that circulate in the blood and can neutralize the pathogen. Infusions of plasma the whitish liquid that remains when blood cel… [+6661 chars]",
"Url": "https://www.washingtonpost.com/health/2020/03/27/coronavirus-serum-plasma-treatment/",
"PublishedAt": "2020-03-27T18:23:07.000Z",
"__v": 0
}
]
正如您在上面看到的,它有两种格式。一个显示带有赞成和反对票的正常帖子,另一个显示新闻。我一直在尝试创造一个未来来获取它,例如:
Future<List<FeedPost>> getPosts () async{
var res= await http.get('$SERVER_IP/Posts/0', headers: {"authorization" : "Token $jwtt"});
if (res.statusCode == 200) {
var postInput = res.body;
var jwtJson = json.decode(postInput.toString());
var jwtString = FeedPost.fromJson(jwtJson);
var postMap = {
'date': jwtString.date,
'author': jwtString.author
};
var author = jwtString.author;
return jwtString;
} else {
throw Exception('Error collecting the data');
}
}
上面的代码是错误的,因为它应该return
是类型的东西List<FeedPost>
FeedPost 是我用来转换第一类帖子的类。
解决方案
推荐阅读
- ios - 开始和结束标签不匹配 - Xcode Storyboard 错误
- angular - 如何从 nativescript Image 或 ImageAsset 获取像素数据
- javascript - 文件夹作为模块在文件夹中作为模块 NodeJS
- asp.net-core - 我的中间件将如何在管道中进行?
- r - 如何更新 r 中列的值而不替换它
- sql-server - 意外令牌“UpdateOptions.ExpandFull”
- java - Java 2D 数组无法将值写入最后一个值索引
- angular - 如何以角度4导出为pdf
- python - 在 keras 模型中使用带有 dropout 的 Native tensorflow RNNLayer
- java - 旅行计划算法的图结构