I have levels screen and I want when I press on level 1 for example I want to get the list of level 1 is there a way to get it without using if or switch just like by passing the name of the list then return the list that I passed its name or something like that
Thanks
List<Map<String, Object>> question(var s)
{
_1 = const [
{
'question':
'In which month does the German festival of Oktoberfest mostly take place?',
'answers': [
{'answerText': 'January', 'score': false},
{'answerText': 'October', 'score': false},
{'answerText': 'September', 'score': true},
],
},
{
'question': 'Who composed the music for Sonic the Hedgehog 3?',
'answers': [
{'answerText': 'Britney Spears', 'score': false},
{'answerText': 'Timbaland', 'score': false},
{'answerText': 'Michael Jackson', 'score': true},
],
},
{
'question': 'In Georgia (the state), it’s illegal to eat what with a fork?',
'answers': [
{'answerText': 'Hamburgers', 'score': false},
{'answerText': 'Fried chicken', 'score': true},
{'answerText': 'Pizza', 'score': false},
],
},
];
return s;
}}
CodePudding user response:
You can try this.. The method question will fetch the item in the index you pass. If you pass 0 it will fetch the first map from the list and return it..
import "dart:async";
void main() {
List<Map<String, Object>> singleObject = question(0);
print(singleObject[0]['question']);
print(singleObject[0]['answers']);
}
List<Map<String, Object>> question(int s)
{
List<Map<String, Object>> _1 = const [
{
'question':
'In which month does the German festival of Oktoberfest mostly take place?',
'answers': [
{'answerText': 'January', 'score': false},
{'answerText': 'October', 'score': false},
{'answerText': 'September', 'score': true},
],
},
{
'question': 'Who composed the music for Sonic the Hedgehog 3?',
'answers': [
{'answerText': 'Britney Spears', 'score': false},
{'answerText': 'Timbaland', 'score': false},
{'answerText': 'Michael Jackson', 'score': true},
],
},
{
'question': 'In Georgia (the state), it’s illegal to eat what with a fork?',
'answers': [
{'answerText': 'Hamburgers', 'score': false},
{'answerText': 'Fried chicken', 'score': true},
{'answerText': 'Pizza', 'score': false},
],
},
];
List<Map<String, Object>> _2 = const [
{
'question':
'In which month does the German festival of Oktoberfest mostly take place?',
'answers': [
{'answerText': 'January', 'score': false},
{'answerText': 'October', 'score': false},
{'answerText': 'September', 'score': true},
],
},
{
'question': 'Who composed the music for Sonic the Hedgehog 3?',
'answers': [
{'answerText': 'Britney Spears', 'score': false},
{'answerText': 'Timbaland', 'score': false},
{'answerText': 'Michael Jackson', 'score': true},
],
},
{
'question': 'In Georgia (the state), it’s illegal to eat what with a fork?',
'answers': [
{'answerText': 'Hamburgers', 'score': false},
{'answerText': 'Fried chicken', 'score': true},
{'answerText': 'Pizza', 'score': false},
],
},
];
List allQuestions = [_1,_2];
return allQuestions[s];
}
CodePudding user response:
You can use maps. The key of the map will be the level name, and the value will be the list of questions.
For Example:
Map<String, List<Map<String, Object>>> leveledQuestionsMap = {
"L1" : [firstLevelQuestions],
"L2" : [secondLevelQuestions],
"L3" : [thirdLevelQuestions],
}
and then make a function that takes level and returns the values from the map:
List<Map<String, Object>> bringQuestionOfLevel(String level){
return leveledQuestionsMap[level];
}