[英]How can i cast a dart map <String, dynamic> to <String, List<List<int>>>?
I read up some data from shared prefs and jsonDecode it into a variable called shapes.我从共享首选项中读取了一些数据并将其 jsonDecode 转换为一个名为 shape 的变量。 When I inspect the shapes in the debugger it looks like the right type.当我在调试器中检查形状时,它看起来是正确的类型。 But when I assign it to the following variable "theShapes" I get errors such as Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, List<List<int>>>'
但是,当我将它分配给以下变量“theShapes”时,我收到错误,例如Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, List<List<int>>>'
static var theShapes = <String, List<List<int>>>{
'general': [
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
],
....
};
The code used to try to cast the "shapes" variable to the type of "theShapes" is like this at the moment:用于尝试将“shapes”变量强制转换为“theShapes”类型的代码目前是这样的:
theShapes = shapes
.map((key, value) => MapEntry(key, List.castFrom(value).toList()));
here you go干得好
go(String data){
final decodedData = jsonDecode(data);
if(decodedData is Map){
return decodedData.map<String,List<List<int>>>((key, value) => MapEntry(key as String, (value as List).map<List<int>>((e) => (e as List).map<int>((i) => i as int).toList()).toList()));
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.