简体   繁体   中英

Flutter/Dart: How to get value in a list of maps?

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.

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.

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']),
        ],
      ],
    );
  }
}

Loop through both questions and answers:

for (var question in questions) {
  print(question['questionText']);
  final answers = question['answers'] as List;
  for (var answer in answers) {
    print(answer['text']); 
  } 
}

Output:

This is the first question?
Answer 1
Answer 2
Answer 3
Answer 4
This is the second question?
Answer 1
Answer 2
Answer 3
Answer 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM