【问题标题】:unable to read data from firestore无法从 Firestore 读取数据
【发布时间】:2020-01-19 21:55:35
【问题描述】:

我尝试使用 json 读取 firestore 中的数据,类型 Map 必须实现可迭代

Stream<List<RentModel>> readData(){
    var json = collection.document("rent").snapshots();
    List<RentModel> rentModel = List();
    return json.map((document) {
      for (var index in document.data){     // error is here in document.data that: the type Map<String, dynamic> must implements iterable 
        rentModel.add(RentModel.fromJson(index));
      }
      return rentModel;
    });
  }

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    如果要使用for...in,则需要有一个实现iterable 的类,例如ListSet,但document.data 的类型为Map

    如果您想在map 中进行迭代,那么您可以使用forEach

    document.data.forEach((key,values) {
      print(key);
    });
    

    https://api.dart.dev/stable/2.3.1/dart-core/Map/forEach.html

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2018-10-26
      相关资源
      最近更新 更多