arrayslistdartgetvalue

Get the value from dart's map


  var bob = {
    'A': 4,
    'B': [4, 5, 6, 7],
    'C': 6
  };
  print(bob['B'][2]);
}

i expect to get 6, but it case error '[]' isn't defined for the class 'Object?'. how to get value from list?


Solution

  • This {} constructor create Map<dynamic, dynamic>, that's why compiler doesn't know what type is there. try cast Object to List, but don't forget to check type.

    final bobB = bob['B'];
    if (bobB is List)
      print(bobB[2]);