0

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;
}}

2 Answers 2

0

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];
}
Sign up to request clarification or add additional context in comments.

3 Comments

it seems like you misunderstand my question I don't want to return the question I want to return the whole list "_1"
So just return _1 instead of _1[s]
for example I have 10 levels when the user choose level 5 it will return _5 so I want a way to return the list of chosen level without use if return for every level
0

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];
}

5 Comments

thanks but when I use the function to get the list it gives me an error ======= this is some of my code List<List<Map<String, Object>>> _questions = Q().bringQuestionOfLevel(e) ; ----- _questions[_questionIndex]['question'],
lib/home.dart:125:48: Error: A value of type 'String' can't be assigned to a variable of type 'int'. _questions[_questionIndex]['question'], ^
lib/home.dart:125:48: Error: A value of type 'String' can't be assigned to a variable of type 'int'. _questions[_questionIndex]['question'], ^ lib/home.dart:125:47: Error: The argument type 'Map<String, Object>' can't be assigned to the parameter type 'String'. - 'Map' is from 'dart:core'. - 'Object' is from 'dart:core'. _questions[_questionIndex]['question'], ^
lib/home.dart:135:46: Error: A value of type 'String' can't be assigned to a variable of type 'int'. ...(_questions[_questionIndex]['answers'] ^
Share the updated code in the question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.