当前位置: 首页 > 知识库问答 >
问题:

Flutter问题:异常(类型_InternalLinkedHashMap不是类型映射的子类型)

钱和平
2023-03-14

我无法理解这个问题。我将下面的文档设置为firesta。我需要将出勤字段转换为列表。

 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()

文件被取出。但模型正在失败。获取异常:[类型“\u 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'];
  }
}

哪里出错了。我需要一点帮助。非常感谢。

共有1个答案

谭文林
2023-03-14

尝试显式转换

Map<String, dynamic> json = doc.data as Map<String, dynamic>;
 类似资料: