简体   繁体   English

我怎样才能投一个飞镖地图<String, dynamic>到<String, List<List<int> &gt;&gt;?

[英]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.

相关问题 Dart:无法转换列表<dynamic>列出<Map<String, dynamic> &gt; 尽管演员 - Dart: cannot convert List<dynamic> to List<Map<String, dynamic>> despite cast 如何转换清单 <Map<String, dynamic> &gt;在地图内 <String, dynamic> 在Dart中转换为JSON? - How to convert a List<Map<String, dynamic>> inside a Map<String, dynamic> to JSON in Dart? 输入“列表<dynamic> ' 不是 'Map 类型的子类型<string, dynamic> ' 在类型转换中</string,></dynamic> - type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast 为什么我应该明确地将我的 JASON object 转换为 Map<string, dynamic> 在 Dart?</string,> - Why should I explicitly cast my JASON object as a Map<String, dynamic> in Dart? 为什么列出 <dynamic> 而不是清单 <Map<String,dynamic> &gt;? - Why List<dynamic> and not List<Map<String,dynamic>>? 如何从列表中查找值<map<string, object> > 在飞镖(扑)? </map<string,> - How to find a value from List<Map<String, Object>> in dart(flutter)? 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter Dart - 如何_InternalLinkedHashMap<string, dynamic> 转换为 Map<string, dynamic> ?</string,></string,> - Dart - how _InternalLinkedHashMap<String, dynamic> convert to Map<String, dynamic>? 颤振:输入&#39;列表<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; 在类型转换中 - Flutter: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast 如何将一些json文件解析为List <Map> 在飞镖? - How can I parse some json files to a List<Map> in dart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM