【问题标题】:Unhandled Exception: type 'ImmutableMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast未处理的异常:类型“ImmutableMap<String, dynamic>”不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2021-02-09 06:44:48
【问题描述】:

我一直在我的颤振应用程序中使用 sembast,但在我的类设备中包含我的类温度列表时遇到问题。我已经包含了下面的两个类。

我得到 Unhandled Exception: type 'ImmutableMap' is not a subtype of type 'List' in type cast 尝试将新设备对象添加到数据库时出现错误,当我尝试从温度列表映射时,它似乎在 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);

【问题讨论】:

  • 你能举个输入的例子吗?
  • 嗨,是的,我已经用添加设备的方式编辑了上面的答案。

标签: flutter dart


【解决方案1】:

发现问题-

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多