错误当我想在主要代码中使用模型 Dart 文件。

huangapple go评论56阅读模式
英文:

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&lt;Question&gt; questionList = [
    Question(&#39;Where is Persian Gulf?&#39;, &#39;Iran&#39;, &#39;Iraq&#39;, 1,
        &#39;assets/images/persiangulf.png&#39;),
    Question(&#39;Which one is most population country in the world?&#39;, &#39;India&#39;,
        &#39;China&#39;, 2, &#39;assets/images/most.png&#39;),
    Question(&#39;Which one created GTA series?&#39;, &#39;Ubisoft&#39;, &#39;Rockstar&#39;, 1,
        &#39;assets/images/GTA.png&#39;),
    Question(&#39;Which one is a rock band?&#39;, &#39;Queen&#39;, &#39;Metallica&#39;, 1,
        &#39;assets/images/guitar.png&#39;),
  ];

}`

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&lt;Question&gt; questionList = [
    Question(&#39;Where is Persian Gulf?&#39;, &#39;Iran&#39;, &#39;Iraq&#39;, 1, &#39;assets/images/persiangulf.png&#39;),
    Question(&#39;Which one is the most populous country in the world?&#39;, &#39;India&#39;, &#39;China&#39;, 2, &#39;assets/images/most.png&#39;),
    Question(&#39;Which one created the GTA series?&#39;, &#39;Ubisoft&#39;, &#39;Rockstar&#39;, 1, &#39;assets/images/GTA.png&#39;),
    Question(&#39;Which one is a rock band?&#39;, &#39;Queen&#39;, &#39;Metallica&#39;, 1, &#39;assets/images/guitar.png&#39;),
  ];

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&lt;Question&gt; questionList = [
    Question(&#39;Where is Persian Gulf?&#39;, &#39;Iran&#39;, &#39;Iraq&#39;, 1, &#39;assets/images/persiangulf.png&#39;),
    Question(&#39;Which one is the most populous country in the world?&#39;, &#39;India&#39;, &#39;China&#39;, 2, &#39;assets/images/most.png&#39;),
    Question(&#39;Which one created the GTA series?&#39;, &#39;Ubisoft&#39;, &#39;Rockstar&#39;, 1, &#39;assets/images/GTA.png&#39;),
    Question(&#39;Which one is a rock band?&#39;, &#39;Queen&#39;, &#39;Metallica&#39;, 1, &#39;assets/images/guitar.png&#39;),
  ];

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.

huangapple
  • 本文由 发表于 2023年6月25日 22:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定