英文:
Error when i want to use model dart file in main code
问题
我正在使用Flutter创建一个问答游戏。
一切都正常工作,直到今天。
我在lib的model目录中创建了一个question_model.dart文件用于问题。
在这个模型文件中,我有一个问题列表,我在其中写问题。
每当我想要使用这个questionList时,它会给我这个错误:
未定义的名称'questionList'。
这是我位于/lib/model/question_model中的questionList:
class Question {
String question;
String answer1;
String answer2;
int trueAnswer;
String imageAddress;
Question(this.question, this.answer1, this.answer2, this.trueAnswer,
this.imageAddress);
List<Question> questionList = [
Question('波斯湾在哪里?', '伊朗', '伊拉克', 1, 'assets/images/persiangulf.png'),
Question('世界上人口最多的国家是哪个?', '印度', '中国', 2, 'assets/images/most.png'),
Question('哪个公司制作了GTA系列游戏?', '育碧', '摇滚之星', 1, 'assets/images/GTA.png'),
Question('哪个是摇滚乐队?', '皇后', '金属乐队', 1, 'assets/images/guitar.png'),
];
}
这是我想要使用这个列表的方式:
checkAnswer() {
setState(() {});
int myAnswer = isOnePressed ? 1 : 2;
questionList[currentQuestionNumber];
}
英文:
I'm creating a quiz game with flutter .
Everything was working fine until today.
I created a question_model dart file in model directory in lib for questions .
In this model file , i have a question List that i write the questions in there.
Everytime that i want to use this questionList it gaves me this error :
Undefined name 'questionList'
It's my questionList which located in /lib/model/question_model:
`class Question {
String question;
String answer1;
String answer2;
int trueAnswer;
String imageAddress;
Question(this.question, this.answer1, this.answer2, this.trueAnswer,
this.imageAddress);
List<Question> questionList = [
Question('Where is Persian Gulf?', 'Iran', 'Iraq', 1,
'assets/images/persiangulf.png'),
Question('Which one is most population country in the world?', 'India',
'China', 2, 'assets/images/most.png'),
Question('Which one created GTA series?', 'Ubisoft', 'Rockstar', 1,
'assets/images/GTA.png'),
Question('Which one is a rock band?', 'Queen', 'Metallica', 1,
'assets/images/guitar.png'),
];
}`
And this is how i want to use this List :
`checkAnswer(){
setState(() {});
int myAnswer = isOnePressed?1:2;
questionList[currentQuestionNumber];
}`
答案1
得分: 0
以下是翻译好的部分:
"An approach would be to declare the question list as static and then use it outside using reference. To do this you can update your code as follows:
static List<Question> questionList = [
Question('Where is Persian Gulf?', 'Iran', 'Iraq', 1, 'assets/images/persiangulf.png'),
Question('Which one is the most populous country in the world?', 'India', 'China', 2, 'assets/images/most.png'),
Question('Which one created the GTA series?', 'Ubisoft', 'Rockstar', 1, 'assets/images/GTA.png'),
Question('Which one is a rock band?', 'Queen', 'Metallica', 1, 'assets/images/guitar.png'),
];
and then reference it in your function in the following way
checkAnswer() {
setState(() {}); // 在这里更新状态
int myAnswer = isOnePressed ? 1 : 2;
Question.questionList[currentQuestionNumber]; // 引用问题列表
}
You may also declare the list outside the question class. That way you donot need to modify anything except for cutting the list out of question class and pasting it down below it."
英文:
An approach would be to declare the question list as static and then use it outside using reference. To do this you can update your code as follows:
static List<Question> questionList = [
Question('Where is Persian Gulf?', 'Iran', 'Iraq', 1, 'assets/images/persiangulf.png'),
Question('Which one is the most populous country in the world?', 'India', 'China', 2, 'assets/images/most.png'),
Question('Which one created the GTA series?', 'Ubisoft', 'Rockstar', 1, 'assets/images/GTA.png'),
Question('Which one is a rock band?', 'Queen', 'Metallica', 1, 'assets/images/guitar.png'),
];
and then reference it in your function in the following way
checkAnswer() {
setState(() {});
int myAnswer = isOnePressed ? 1 : 2;
Question.questionList[currentQuestionNumber];
}
You may also declare the list outside the question class. That way you donot need to modify anything except for cutting the list out of question class and pasting it down below it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论