Flutter将Firebase Realtime数据库数据检索到Flutter模型

时间:2020-10-31 07:37:51

标签: database firebase flutter dart model

我想在flutter模型中存储Firebase实时数据库数据,所以这是我的模型

class MessageData extends Equatable {
  String chatId;
  String recipientName; 
  String recipientStatus; 
  bool selected; 
  String senderName; 

  MessageData({
    this.chatId,
    this.recipientName, 
    this.recipientStatus, 
    this.selected, 
    this.senderName, 
  });

  factory MessageData.fromJson(Map<dynamic, dynamic> json) => MessageData(
      chatId: json["chatId"],
      recipientName: json["recipientName"], 
      recipientStatus: json["recipientStatus"], 
      selected: json["selected"], 
      senderName: json["senderName"], 
      
  @override
  List<Object> get props =>
    [chatId,
    recipientName, 
    recipientStatus, 
    senderName, 
    senderStatus, 
    ];

}

这是从Firebase检索的代码

await FirebaseDatabase.instance
  .reference()
  .child("inbox")
  .child(userId)
  .then((DataSnapshot snapshot){
    Map<dynamic, dynamic> values = snapshot.value;
    print(values.toString());                      //print 1
    var a = MessageData.fromJson(values);
    print(a);                                      //print 2
  });

及以下是印刷品1的结果

{
89: {chatId: 122-89, senderName: renjun, recipientName: mark leee, id: -MKAJs-26cyv35JhF9gP, selected: false}, 
42: {chatId: 122-42, senderName: taplak, recipientName: renjun, id: -MKAS0l4mfQiWCi_xpmD, selected: false}
}

及以下是印刷品2的结果

I/flutter (26534): null

我怎么知道Firebase中的数据何时已经存储在模型中?或任何人有任何建议?如果数据已成功存储在模型中,我想显示ListView.builder()

的收件人姓名列表

0 个答案:

没有答案