0

How to access a Map element value by key, when a Map is inside a list?

'rooms': [
        {
          'roomNumber': 1,
          'roomBeds': 1,
          'roomColor': 1,
          'roomNumOfDwellers': 0,
          'roomDwellers': [
          ]
        },
        {
          'roomNumber': 2,
          'roomBeds': 3,
          'roomColor': 2,
          'roomNumOfDwellers': 0,
          'roomDwellers': [
          ]
        },
      ]

Tried this, doesn't work:

myPODO.rooms[1].['roomBeds']

The error message is:

Class '_InternalLinkedHashMap' has no instance getter 'roomBeds'. Receiver: _LinkedHashMap len:5 Tried calling: roomBeds

1
  • Can we see how your getting myPODO? Commented Apr 29, 2020 at 11:22

2 Answers 2

4

try to replace

myPODO.rooms[1].['roomBeds']

with

myPODO.rooms[1]['roomBeds']
Sign up to request clarification or add additional context in comments.

Comments

3

Store the items in a list of Map and you can then print the values by key names.

void main() {

  List<Map<String, dynamic>> rooms = [
        {
          'roomNumber': 1,
          'roomBeds': 1,
          'roomColor': 1,
          'roomNumOfDwellers': 0,
          'roomDwellers': [
          ]
        },
        {
          'roomNumber': 2,
          'roomBeds': 3,
          'roomColor': 2,
          'roomNumOfDwellers': 0,
          'roomDwellers': [
          ]
        },
      ];

  print(rooms[1]['roomBeds']);
}

1 Comment

It worked, thank you very much! The problem was in the dot between rooms[1] and ['roomBeds'], it wasn't needed.

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.