【问题标题】:Trying to get an firestore snapshot with an type cast尝试使用类型转换获取 Firestore 快照
【发布时间】:2019-09-27 07:39:08
【问题描述】:

我正在尝试从 firestore 获取一些格式化数据,但出现此错误:

引发了另一个异常:类型“_InternalLinkedHashMap”不是类型转换中“Store”类型的子类型

这是我的课程代码。

class StoreInterface {
  String name;
  String address;
}

class Store{

  Store data;

  Store.fromDocument(DocumentSnapshot snapshot){

    StoreInterface data = snapshot.data as StoreInterface
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    snapshot.data 是一个Map<String, dynamic>,不能转换为StoreInterface

    我建议向StoreInterface 添加一个构造函数,该构造函数接受地图。

    class StoreInterface {
    
      String name;
      String address;
    
      StoreInterface.fromMap(Map<String, dynamic> data)
        : name = data['name'],
          address = data['address'];
    }
    

    像这样使用它:

    StoreInterface data = StoreInterface.fromMap(snapshot.data);
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2019-10-17
      • 2020-06-09
      • 2019-07-26
      • 2023-03-19
      • 2021-11-24
      • 2018-08-22
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多