未处理的异常:类型“ {mm44,动态>”不是铸造类型

时间:2020-10-27 00:31:59

标签: flutter dart

我在flutter应用程序中一直使用sembast,但在将我的班级温度列表包含在我的设备类中时遇到问题。我在下面都包括了这两个课程。

我得到了未处理的异常:尝试将新的Equipment对象添加到数据库时,类型'ImmutableMap '不是强制转换类型'List'的子类型,当我尝试从“温度”列表中进行映射时,它似乎在Equipment.fromMap中。

此方法似乎用于解决与我的问题几乎相同的其他问题的解决方案,因此,我不确定这里出现了什么问题以及我尝试过的其他解决方案。

class Equipment {
  final int id;
  final String name;
  final String location;
  final String zone;
  final int checkInterval;
  final String nextUpdate;
  final List<Temperature> temps;



  Equipment({this.id, this.name, this.location, this.zone, this.checkInterval, this.nextUpdate, this.temps});

  Map<String, dynamic> toMap() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    if (this.temps != null) {
      data['temps'] = this.temps.map((v) => v.toMap()).toList();
    }
    return {
      'name': this.name,
      'location': this.location,
      'zone': this.zone,
      'checkNumber':this.checkInterval,
      'nextUpdate':this.nextUpdate,
      'temps':data
    };
  }


  factory Equipment.fromMap(int id, Map<String, dynamic> map) {

    return Equipment(
      id: id,
      name: map['name'],
      location: map['location'],
      zone: map['zone'],
      checkInterval: map['checkNumber'],
      nextUpdate: map['nextUpdate'],
      temps: map['temps'] != null
          ? (map['temps'] as List).map((i) => Temperature.fromMap(i)).toList()
          : null,
    );
  }
}
class Temperature {
  final String time;
  final int temp;



  Temperature({this.time, this.temp});

  Map<String, dynamic> toMap() {
    return {
      'time': time,
      'temp': temp,
    };
  }



  factory Temperature.fromMap(Map<String, dynamic> map) {
    return Temperature(
      time: map['time'],
      temp: map['temp'],

    );
  }
}
A typical input to equipment-

final name = nameController.text;
                  final location = locationController.text;
                  final zone = zoneController.text;
                  final checkInterval = int.parse(intervalController.text);
                  final nextUpdate = DateTime.now().add(new Duration(hours: checkInterval)).toString();
                  final Temperature temp = Temperature(time: DateTime.now().add(new Duration(hours: checkInterval)).toString(),temp: 0);
                  final  List<Temperature> temps = [temp,temp];
                  final newEquipment = Equipment(name: name, location: location, zone: zone, checkInterval: checkInterval, nextUpdate: nextUpdate, temps: temps);

1 个答案:

答案 0 :(得分:0)

发现了问题-

? (map['temps'] as List).map((i) => Temperature.fromMap(i)).toList()

map ['temps']不是列表,而是地图,应该是map ['temps'] ['temps']