扑动/Dart:如何在地图列表中获得值?



这可能是一个非常简单的问题,但我找不到解决方案。如何得到"答案1"、"答案2"等等。从这张地图,并把他们在循环文本小部件?我想做问卷调查,我需要得到"文本"的值。

我尽可能地简化了代码:

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

尝试:questions[questionIndex]['answers']['text']等,但不工作。

您可以使用questions[index]["questionText"];获得特定问题

及其答案questions[index]["answers"] as List?;

下面是示例

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

如果您为它创建模型类会更好。

您可以访问您的答案并以以下方式列出它们:

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

遍历问题和答案:

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

输出:

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

最新更新