At listAnswers[1] i did try to assign the string 'white' at the text view but it crashes my program, anyone have any idea why, here is the Class questions which contains a string question, list with answers and a string with correct answer to evaluate, and a quiz class to build the quiz.
And the error log is NoSuchMethodError: The method '[]' was called on null. Reciever: Null Tried Calling: [] (1)
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);
}
Try this.
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);
}
Your List<String> _answers
is not initialized which means it is equal to null. You need to give it an initial value =[]
or =new List()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.