gpt4 book ai didi

android - 将JSON字符串转换为flutter中的列表对象

转载 作者:行者123 更新时间:2023-12-03 04:04:10 25 4
gpt4 key购买 nike

我对 Dart 还是陌生的。现在,我正在尝试从REST API中提取数据。按照我编写的脚本:

class ChatCard {
final String chatdetail_id;
final String chatdetail_userfullname;
final String chatdetail_userurlphoto;
final String chatdetail_message;
final int chatdetail_isread;
final String chatdetail_datetime;

ChatCard({
this.chatdetail_id,
this.chatdetail_userfullname,
this.chatdetail_userurlphoto,
this.chatdetail_message,
this.chatdetail_isread,
this.chatdetail_datetime
});

factory ChatCard.fromJson(Map<String, dynamic> json) {
return new ChatCard(
chatdetail_id : json['chatdetail_id'] as String,
chatdetail_userfullname : json['chatdetail_userfullname'] as String,
chatdetail_userurlphoto : json['chatdetail_userurlphoto'] as String,
chatdetail_message : json['chatdetail_message'] as String,
chatdetail_isread : json['chatdetail_isread'] as int,
chatdetail_datetime : json['chatdetail_datetime'] as String
);
}
}

class ChatCardList extends StatefulWidget {

List<ChatCard> chatcard;

ChatCardList({Key key, this.chatcard}) : super(key: key);

@override
_ChatCardListState createState() => new _ChatCardListState(chatcard:chatcard);

ChatCardList.tambah(String message) {
print("RESULT MESSAGE = " + message); //result on console : [{"createdAt":"2018-08-16T02:38:37.757Z","is_read":1,"_id":"5b74e3ad26c7de02ed664dd2","from_id":"5b74d3f5e0da63027ab03664","from_name":"Ahmad Adiwijaya","from_photo":"","from_device":"Mobile Phone","text":"Kami dari sekolah tinggi yg hanya satu prodi. Apakah lembaga penjaminan mutuh harus 2, yaitu LPM pada level institusi dan UPM pada level prodi?"},{"from_id":"SystemDate","from_name":"System","from_photo":"","text":"2018-08-23T11:26:53.968Z"}]

}
}

我的问题是,如何将上面的“RESULT MESSAGE”结果数据输入到 List<ChatCard> chatcard类中?

多谢,

最佳答案

您是否期望这样的事情,

ChatCardList.tambah(String message) {
List<ChatCard> chatCardList = message
.map((chatCardJson) => ChatCard.fromJson(chatCardJson))
.toList();
print(chatCardList); // [Instance of 'ChatCard', Instance of 'ChatCard']
}

注意:但是控制台中打印的json中的 keyskeys中的 ChatCard不同。您可能需要重新审视。

关于android - 将JSON字符串转换为flutter中的列表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52225846/

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