【问题标题】:Firestore document to Flutter Problem : Exception( type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>')Firestore 文档到 Flutter 问题:异常(类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型 'Map<String, dynamic>' 的子类型)
【发布时间】:2020-05-31 18:45:15
【问题描述】:

无法解决这个问题。 我将以下文档设置为 Firestore。 我需要将“出席”字段转换为列表。

 Map<String, dynamic> data = {
  "name": "memberName",
  "attendance": {
    "0": {"morning": false, "noon": true, "night": true},
    "1": {"morning": false, "noon": true, "night": true}
  },
  "deposits": {
    "0": {"date": "date", "amount": 100},
    "1": {"date": "date", "amount": 100}
  }
};

使用 StreamProvider 。流没有问题。 使用 toList()

文件被提取。但是模型失败了。获得异常:[类型'_InternalLinkedHashMap'不是'Map'类型的子类型] 模型类:

class Member {
  String id;
  String name;
  List<Attendance> attendances;
  List<Deposit> deposits;

  Member({this.id, this.name, this.attendances, this.deposits});

  Member.fromSnapshot(DocumentSnapshot doc) {
    Map<String, dynamic> json = doc.data;
    id = doc.documentID;
    name = json['name'];
    if (json['attendance'] != null) {
      Map<String, dynamic> atdmap = json['attendance'];
      attendances = new List<Attendance>();
      atdmap.forEach((k, v) {
        attendances.add(new Attendance.fromJson(v));
      });
    }
    if (json['deposits'] != null) {
      Map<String, dynamic> dmap = json['deposits'];
      deposits = new List<Deposit>();
      dmap.forEach((k, v) {
        deposits.add(new Deposit.fromJson(v));
      });
    }
  }
}

class Attendance {
  bool morning;
  bool noon;
  bool night;

  Attendance({this.morning, this.noon, this.night});

  Attendance.fromJson(Map<String, dynamic> json) {
    morning = json['morning'];
    noon = json['noon'];
    night = json['night'];
  }
}

class Deposit {
  String date;
  int amount;

  Deposit({this.date, this.amount});

  Deposit.fromJson(Map<String, dynamic> json) {
    date = json['date'];
    amount = json['amount'];
  }
}

哪里出错了。需要一点帮助。 谢谢。

【问题讨论】:

    标签: flutter dart model provider


    【解决方案1】:

    尝试显式转换

    Map<String, dynamic> json = doc.data as Map<String, dynamic>;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 2019-04-03
      • 2020-08-20
      • 2018-09-24
      • 2021-07-11
      • 2021-01-17
      • 2019-03-23
      • 2020-11-30
      相关资源
      最近更新 更多