Reputation: 11
I've been using sembast in my flutter application and I'm having issues with including a list of my class Temperature in my class Equipment. I have included both classes below.
I get the Unhandled Exception: type 'ImmutableMap<String, dynamic>' is not a subtype of type 'List' in type cast error when trying to add a new Equipment object to the database, and it seems to be in Equipment.fromMap when I try to map from the Temperature list.
This method seems to be used in solutions to other issues that seem near identical to mine, so I'm not sure what's failing here, as well as in the other solutions I've tried.
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);
Upvotes: 0
Views: 941
Reputation: 11
Found the issue-
? (map['temps'] as List).map((i) => Temperature.fromMap(i)).toList()
map['temps'] is not a list but a map, it should be map['temps']['temps']
Upvotes: 1