gpt4 book ai didi

dart - 无法弄清楚如何在 Flutter 的多项选择测验中从 List 分配正确的答案值,

转载 作者:IT王子 更新时间:2023-10-29 07:10:11 25 4
gpt4 key购买 nike

在 listAnswers[1] 中,我确实尝试在 TextView 中分配字符串 'white' 但它使我的程序崩溃,任何人都知道为什么,这里是包含字符串问题的类问题,包含答案的列表和带有要评估的正确答案的字符串,以及用于构建测验的测验类。
错误日志是 NoSuchMethodError: The method '[]' was called on null.接收者:空尝试调用:[] (1)

class _MyHomePageState extends State<MyHomePage> {
Questions currentQuestion;

Quiz quiz = new Quiz([
new Questions(
"Color of the snow is ", ["yellow", "white", "grey"], "white"),]);
String questionText;
int questionNumber;
String isCorrect;
List<String> listAnswers;

@override
void initState() {
super.initState();
currentQuestion = quiz.nextQuestion;
questionText = currentQuestion.question;
questionNumber = quiz.questionNumber;
listAnswers = quiz.answers;
isCorrect = quiz.correctAnswer;
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new InkWell(
child: Center(
child: new Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: new Text(
questionText,
maxLines: questionNumber,
style: new TextStyle(fontSize: 20.0),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: new RaisedButton(
// child: new Text(Questions.listAnswers.toString()),
child: new Text(
quiz.answers.toString(),
maxLines: questionNumber,
),
onPressed: _onPressed,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: new Text(
listAnswers[0],
style: new TextStyle(fontSize: 20.0),
),
),
],
),
),
),
);
}
}

class Quiz {
List<Questions> _questions;
int _currentQuestionIndex = -1;
int _point = 0;
List<String> _answers;
String _correctAnswer;

Quiz(this._questions) {
_questions.shuffle();
}

List<Questions> get questions => _questions;
List get answers => _answers;
String get correctAnswer => _correctAnswer;
int get length => _questions.length;
int get questionNumber => _currentQuestionIndex + 1;
int get point => _point;

Questions get nextQuestion {
_currentQuestionIndex++;
if (_currentQuestionIndex >= length) return null;
return _questions[_currentQuestionIndex];
}
}

class Questions {
final String question;
final List<String> answers;
final String correctAnswer;

Questions(this.question, this.answers, this.correctAnswer);
}

最佳答案

试试这个。

import 'package:flutter/material.dart';

void main() => runApp(new MaterialApp(
home: new MainPage(),
debugShowCheckedModeBanner: false,
));

class MainPage extends StatefulWidget {
@override
_MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPage> {
Questions currentQuestion;

Quiz quiz = new Quiz([
new Questions(
"Color of the snow is ", ["yellow", "white", "grey"], "white"),
]);
String questionText;
int questionNumber;
String isCorrect;
List<String> listAnswers;

@override
void initState() {
super.initState();
currentQuestion = quiz.nextQuestion;
questionText = currentQuestion.question;
questionNumber = quiz.questionNumber;
listAnswers = quiz.answers;
isCorrect = quiz.correctAnswer;
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Quiz'),
),
body: new InkWell(
child: Center(
child: new Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: new Text(
questionText,
maxLines: questionNumber,
style: new TextStyle(fontSize: 20.0),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: buildAnswerButtons(0),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: new Text(
quiz._questions[0].correctAnswer,
style: new TextStyle(fontSize: 20.0),
),
),
],
),
),
),
);
}

Widget buildAnswerButtons(int questionPos) {
List<Widget> buttons = [];
for (String answer in quiz._questions[questionPos].answers) {
buttons.add(
new RaisedButton(
child: new Text(answer),
onPressed: () {},
),
);
}
return new Row(
mainAxisSize: MainAxisSize.min,
children: buttons,
);
}
}

class Quiz {
List<Questions> _questions;
int _currentQuestionIndex = -1;
int _point = 0;
List<String> _answers;
String _correctAnswer;

Quiz(this._questions) {
_questions.shuffle();
}

List<Questions> get questions => _questions;

List get answers => _answers;

String get correctAnswer => _correctAnswer;

int get length => _questions.length;

int get questionNumber => _currentQuestionIndex + 1;

int get point => _point;

Questions get nextQuestion {
_currentQuestionIndex++;
if (_currentQuestionIndex >= length) return null;
return _questions[_currentQuestionIndex];
}
}

class Questions {
final String question;
final List<String> answers;
final String correctAnswer;

Questions(this.question, this.answers, this.correctAnswer);
}

关于dart - 无法弄清楚如何在 Flutter 的多项选择测验中从 List<String> 分配正确的答案值,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com