【问题标题】:How can I convert the documents in a Firebase Collection into a Map (in Flutter)?如何将 Firebase 集合中的文档转换为地图(在 Flutter 中)?
【发布时间】:2020-04-11 02:02:57
【问题描述】:

正如标题所说。我真的无法想象过去问题所述的每种快照方法之间的差异。使用快照和 StreamBuilders 时,我不断得到空值。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    只需像这样创建模型:

    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class EmployeeData {
      final DocumentReference reference;
      String address;
      String designation;
    
      EmployeeData.data(
         this.reference, [
         this.address,
         this.designation,
     ]);
    
     factory EmployeeData.from(DocumentSnapshot document) => EmployeeData.data(
        document.reference,
        document.data['Address'],
        document.data['Designation'],
      );
    
     void save() {
      reference.setData(toMap());
     }
    
    Map<String, dynamic> toMap() {
      return {
      'address': address,
      'designation': designation,
     };
     }
    }
    

    然后像这样传递文件:

    Map map = EmployeeData.from(snapshot).toMap()
    

    【讨论】:

    • 谢谢。但是我应该在哪里传递代码中的最后一条语句?
    • 可能在StreamBuilder 或者你可以访问数据快照的地方
    • 非常感谢!第二个链接中的解决方案是我正在寻找的。现在完美运行,我离理解流更近了一步。
    猜你喜欢
    • 2020-03-19
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2022-01-08
    相关资源
    最近更新 更多