1

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()));
2
  • Try this: final shapesMap = shapes.map((key, value) => MapEntry(key, List<List<int>>.from(value))); theShapes = Map<String, List<List<int>>>.from(shapesMap); Commented Nov 18, 2020 at 9:20
  • Nice try but that code throws this error: Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<int>' on the first line. Commented Nov 18, 2020 at 11:02

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.