在listAnswers [1]我确实尝试分配字符串' white'在文本视图但它崩溃了我的程序,任何人都知道为什么,这里是包含字符串问题的类问题,带有答案的列表和具有正确答案评估的字符串,以及用于构建测验的测验类。 />
错误日志为NoSuchMethodError: The method '[]' was called on null. Reciever: Null Tried Calling: [] (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);
}
答案 0 :(得分:2)
试试这个。
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);
}
答案 1 :(得分:1)
您的List<String> _answers
未初始化,这意味着它等于null。您需要为其指定初始值=[]
或=new List()