I suppose you want to incorporate the answers within the questions, like this:
1: This is question 1
Desc: first_question
Answer:answer for question 1
2: This is question 2
Desc: second_question
Answer:Missing answer
3: This is question 3
Desc: third
Answer:answer for question 3
4: This is question 4
Desc: fourth
Answer:Missing answer
5: This is question 5
Desc: fifth
Answer:answer for question 5
6: This is question 6
Desc: sixth
Answer:Missing answer
Here is a solution using map
on the List
of Question
:
void main() {
List<Question> combined = questions
.map((question) => question
..answerText =
answers.firstWhere((answer) => answer.id == question.id, orElse: () => null)?.answerText ?? 'Missing answer')
.toList();
combined.forEach((question) => print(question));
}
class Question {
dynamic id;
final String label;
final String description;
String answerText;
Question({
this.id,
this.label,
this.description,
this.answerText,
});
toString() => '$id: $label\nDesc: $description\nAnswer:$answerText\n';
}
List<Question> questions = [
Question(id: 1, label: 'This is question 1', description: 'first_question'),
Question(id: 2, label: 'This is question 2', description: 'second_question'),
Question(id: 3, label: 'This is question 3', description: 'third'),
Question(id: 4, label: 'This is question 4', description: 'fourth'),
Question(id: 5, label: 'This is question 5', description: 'fifth'),
Question(id: 6, label: 'This is question 6', description: 'sixth'),
];
List<Question> answers = [
Question(id: 1, answerText: 'answer for question 1'),
Question(id: 3, answerText: 'answer for question 3'),
Question(id: 5, answerText: 'answer for question 5'),
];