It’s propably very simple question, but i can’t find a solution. How to get a ‘Answer 1’, ‘Answer 2’,etc. from this map and put them in to Text widget in loop? I’m trying to make questionnaire and i need to get values of ‘text’.
I simplified the code as much as possible:
final questions = const [
{
'questionText': 'This is the first question?',
'answers': [
{'text': 'Answer 1', 'answer': 1},
{'text': 'Answer 2', 'answer': 2},
{'text': 'Answer 3', 'answer': 3},
{'text': 'Answer 4', 'answer': 4},
],
},
{
'questionText': 'This is the second question?',
'answers': [
{'text': 'Answer 1', 'answer': 1},
{'text': 'Answer 2', 'answer': 2},
{'text': 'Answer 3', 'answer': 3},
{'text': 'Answer 4', 'answer': 4}
],
},
];
int numberOfAnswers = 4;
int questionIndex = 0;
Column(
children: [
for (var i = 0; i < numberOfAnswers; i++)
Text('Answer (1,2,3,4)'),
],
),
Tried: questions[questionIndex][‘answers’][‘text’], etc, but it doesn’t work.
3
Answers
You can get specific question with
questions[index]["questionText"];
and its answer
questions[index]["answers"] as List?;
Here is the example
It would be better if you create model class for it.
You can access your answers and list them the following way:
Loop through both questions and answers:
Output: