I have some data in this form as below located at the start of the file
const _questions = [
{
'question': 'How long is New Zealand’s Ninety Mile Beach?',
'answers': [
'88km, so 55 miles long.',
'55km, so 34 miles long.',
'90km, so 56 miles long.'
],
'answer': 1,
},
{
'question':
'In which month does the German festival of Oktoberfest mostly take place?',
'answers': ['January', 'October', 'September'],
'answer': 2,
},
{
'question': 'Who composed the music for Sonic the Hedgehog 3?',
'answers': [
'Britney Spears',
'Timbaland',
'Michael Jackson',
],
'answer': 1,
},
]
I have a class like this
class QuestionNumber with ChangeNotifier {
List<int> answerlist= [1, 2, 1];
}
I want to initialise the list to have all of the 'answer' numbers so that when that the list is initialised to List answerlist= [];
Thanks for any help!
CodePudding user response:
You need a model class to handle this(easy approach), and in some cases you don't have answer
filed, for that I'm using default value 0,
Model Class
class Question {
final String question;
final List<String> answers;
final int answer;
Question({
required this.question,
required this.answers,
required this.answer,
});
Map<String, dynamic> toMap() {
return {
'question': question,
'answers': answers,
'answer': answer,
};
}
factory Question.fromMap(Map<String, dynamic> map) {
return Question(
question: map['question'] ?? '',
answers: List<String>.from(map['answers']),
answer: map['answer']?.toInt() ?? 0,
);
}
String toJson() => json.encode(toMap());
factory Question.fromJson(String source) =>
Question.fromMap(json.decode(source));
}
It is more like parsing json
Then do like
List<Question> questionlist =
_questions.map((q) => Question.fromMap(q)).toList();
List<int> answerlist = [];
for (final q in questionlist) {
answerlist.add(q.answer);
}
Check on dartPad
CodePudding user response:
check this below it may help you,
DataModel,
class QuestionsModel {
List<Lists> lists;
QuestionsModel({this.lists});
QuestionsModel.fromJson(Map<String, dynamic> json) {
if (json['lists'] != null) {
lists = new List<Lists>();
json['lists'].forEach((v) {
lists.add(new Lists.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.lists != null) {
data['lists'] = this.lists.map((v) => v.toJson()).toList();
}
return data;
}
}
class Lists {
String question;
List<String> answers;
int answer;
Lists({this.question, this.answers, this.answer});
Lists.fromJson(Map<String, dynamic> json) {
question = json['question'];
answers = json['answers'].cast<String>();
answer = json['answer'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['question'] = this.question;
data['answers'] = this.answers;
data['answer'] = this.answer;
return data;
}
}
QuestionsModel questionsModel = new QuestionsModel.fromJson({
"lists": [
{
"question": "How long is New Zealand’s Ninety Mile Beach?",
"answers": [
"88km, so 55 miles long.",
"55km, so 34 miles long.",
"90km, so 56 miles long."
],
"answer": 1
},
{
"question":
"In which month does the German festival of Oktoberfest mostly take place?",
"answers": ["January", "October", "September"],
"answer": 2
},
{
"question": "Who composed the music for Sonic the Hedgehog 3?",
"answers": ["Britney Spears", "Timbaland", "Michael Jackson"],
"answer": 1
}
]
});
To get List of answer number from ClassModel,
var answers = questionsModel.lists.map((e) => e.answer);
print("Answers Number : ${answers}"); // 1, 2, 1
To get object value from Class using list builder,
ListView.builder(
itemCount: questionsModel.lists.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(questionsModel.lists[index].question), // access value like this
);
},
)