[英]flutter: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'
[英]How to convert Map<String, dynamic> to Map<int, String> in flutter
这是预期的 map
{
1: '1000',
2: '400',
3: '800',
4: '7000',
5: '5000',
6: '300',
7: '2000',
8: '100',
};
这是我的 model系列,特别是 map
class PackageModel {
String? id;
final String? name;
String? description;
int? price;
String? pcolor;
String? img;
Map? range;
PackageModel({
this.id,
this.name,
this.description,
this.price,
this.img,
this.pcolor,
this.range,
});
static PackageModel fromJson(Map<String, dynamic> json) => PackageModel(
id: json['id'],
name: json['name'],
description: json['description'],
price: json['price'],
img: json['img'],
pcolor: json['pcolor'],
range: json['range'],
);
}
现在我想在这里使用范围:
updatePackageRange(pack.range as Map<int, String>);
但是我遇到了这个问题
Exception has occurred.
_CastError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<int, String>' in type cast)
如何使这个<String, dynamic>成为<int, String>
您需要更具体地转换range
字段,并且range
也应该具有Map<int, String>
类型。
class 的更新版本可能如下所示:
class PackageModel {
String? id;
final String? name;
String? description;
int? price;
String? pcolor;
String? img;
Map<int, String>? range;
PackageModel({
this.id,
this.name,
this.description,
this.price,
this.img,
this.pcolor,
this.range,
});
static PackageModel fromJson(Map<String, dynamic> json) => PackageModel(
id: json['id'],
name: json['name'],
description: json['description'],
price: json['price'],
img: json['img'],
pcolor: json['pcolor'],
range: json['range'].map<int, String>(
(key, value) =>
MapEntry<int, String>(int.parse(key), value.toString()),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.