0
  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?

1 Answers1

0

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]);
Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • is right here. @bogdan1992 if you are sure it will always be `List` at `bob['B']` then you can use ` var bobB = bob['B'] as List; print(bobB[2]);` – TheAlphamerc Jun 18 '21 at 13:49