I have a quick question and it's an easy one. Please somebody help.
I am an absolute beginner in flutter. I am having trouble accessing the object inside the list and their size. Here is a quick example of what I am trying to achieve.
final questions = [
{
'questionText': 'What's your favorite color?',
'answers': [
{'text': 'Black', 'score': 3},
{'text': 'Blue', 'score': 1},
{'text': 'White', 'score': 1}
]
},
{
'questionText': 'What's your favorite animal?',
'answers': [
{'text': 'Lion', 'score': 3},
{'text': 'Snake', 'score': 5},
{'text': 'Cat', 'score': 1}
]
},
{
'questionText': 'What's your favorite teacher?',
'answers': [
{'text': 'Ali', 'score': 3},
{'text': 'Aslam', 'score': 3},
{'text': 'Akram', 'score': 3}
]
}
];
print(questions.length); // 3
print(questions[0].length); // 2
print(questions[0]['answers']); //[{text: Black, score: 3}, {text: Blue, score: 1}, {text: White, score: 1}]
But when I tried to get the length of "answers" within the questions by following below method. I get this error (given inside comments).
print(questions[0]['answers'].length); //Error: The getter 'length' isn't defined for the class 'Object'.
When I tried to print the first element of 'answers' array within the questions (which is "{'text': 'Black', 'score': 3}"), I get the below error
print(questions[0]['answers'][0]); // The method '[]' isn't defined for the class 'Object'
Also tried these
print(questions[0]['answers']['text']); // Error: The method '[]' isn't defined for the class 'Object'.
print(questions[0]['answers'[0]]); // null
print(questions[0]['answers'['text']]); // Error: The argument type 'String' can't be assigned //to the parameter type 'int'.
How can I do this? Also, what should I do if I only want to print all three options('text') of 'answers'. I know it's a kinda dumb question but I am completely stuck here. Any help would highly be appreciated.