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.
CodePudding user response:
You can get specific question with questions[index]["questionText"];
and its answer questions[index]["answers"] as List?;
Here is the example
class Fasd4 extends StatelessWidget {
const Fasd4({super.key});
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}
],
},
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: questions.length,
itemBuilder: (context, index) {
final question = questions[index]["questionText"];
final answers = questions[index]["answers"] as List?;
return Column(
children: [
Container(
height: kToolbarHeight,
color: Colors.deepPurple,
alignment: Alignment.center,
child: Text("$question")),
for (var i = 0; i < (answers?.length ?? 0); i )
Text(
"text: ${answers?[i]['text']} : answer ${answers?[i]['answer']} "),
],
);
},
));
}
}
It would be better if you create model class for it.
CodePudding user response:
You can access your answers and list them the following way:
class TestWidget extends StatelessWidget {
TestWidget({Key? key}) : super(key: key);
final List<Map<String, dynamic>> questions = [
{
'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}
],
},
];
final int numberOfAnswers = 4;
final int questionIndex = 0;
@override
Widget build(BuildContext context) {
return Column(
children: [
for (int i = 0; i < numberOfAnswers; i ) ...[
Text(questions[questionIndex]['answers'][i]['text']),
],
],
);
}
}