Reputation: 137
I'm attempting to extract a value from list contained in a list of maps, but am getting the following error: The operator '[]' isn't defined for the type 'Object'
From the following list, I'd like to access a value such as 'Pizza':
List<Map<String,Object>> questions = [
{
'question': 'What is your favorite food',
'answers': [
'Pizza',
'Tacos',
'Sushi',
],
},
];
When I try to print an answer within the list of answers, it doesn't work:
// Does not work
// The operator '[]' isn't defined for the type 'Object'
print(questions[0]['answers'][0]);
I'm able to save the answers list into a variable to of type list then print a specific list item:
// Works
List answerList = questions[0]['answers'];
print(answerList[0]);
Why doesn't the first way work, and how can I get this to work with one command?
Upvotes: 0
Views: 1036
Reputation: 1670
Rather than returning an Object
return a dynamic
as it has an operator []
List<Map<String, dynamic>> questions = [
{
'question': 'What is your favorite food',
'answers': [
'Pizza',
'Tacos',
'Sushi',
],
},
];
Upvotes: 2