lost baby
lost baby

Reputation: 3268

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. 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>>>'

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:

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

Upvotes: 1

Views: 256

Answers (1)

Yadu
Yadu

Reputation: 3305

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

Upvotes: 1

Related Questions