>>?,json,flutter,dart,casting"/>

首页 > 解决方案 > 我怎样才能投出飞镖图>>?

问题描述

我从共享首选项中读取了一些数据并将其 jsonDecode 到一个名为形状的变量中。当我在调试器中检查形状时,它看起来像是正确的类型。但是当我将它分配给以下变量“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],
],
....
};

用于尝试将“shapes”变量转换为“theShapes”类型的代码目前是这样的:

 theShapes = shapes
      .map((key, value) => MapEntry(key, List.castFrom(value).toList()));

标签: jsonflutterdartcasting

解决方案


干得好



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;
}

推荐阅读