OOP 错误行的子级不得包含任何空值,但在索引 0 处找到空值



我需要从main中移动记分员。冲到问题库去。然而,这个错误一直出现,我不知道在哪里以及如何修复它。必须应用面向对象的概念,但我很困惑从哪里开始以及如何去做。我太忙了。

import 'package:flutter/material.dart';
import 'questionbank.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
QuestionBank qb = new QuestionBank();
void main() => runApp(Quizzler());
class Quizzler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey.shade900,
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: QuizPage(),
),
),
),
);
}
}
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
void checkAnswer (bool A){
setState(() {
if(qb.isFinished()==true){
Alert(
context: context,
title: 'End of Questions',
desc: 'It will start again.',
style: const AlertStyle(
titleStyle: TextStyle(fontWeight: FontWeight.bold),
descStyle: TextStyle(fontSize: 15),
),
buttons: [
DialogButton(
child: Text(
"OKAY",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
qb.reset();
qb.scoreKeeper = [];
} else{
if(qb.getSagot()==A){
qb.scoreKeeper.add(
qb.skcorrect()
);
}
else{
qb.scoreKeeper.add(
qb.skwrong(),
);
}
qb.next();
}
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
qb.getTanong(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: Container(
color: Colors.green,
child: TextButton(
child: Text(
'True',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onPressed: () {
checkAnswer(true);
},
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: Container(
color: Colors.red,
child: TextButton(
child: Text(
'False',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () {
checkAnswer(false);
},
),
),
),
),
Row(
children:
qb.scoreKeeper,
),
],
);
}

}

import 'package:flutter/material.dart';
import 'question.dart';
class QuestionBank{
int item = 0;
List<Question> questions = [
Question('You can lead a cow down stairs but not up stairs.', false),
Question('Approximately one quarter of human bones are in the feet.', true),
Question('A slug's blood is green.', true),
Question('Buzz Aldrin's mother's maiden name was "Moon".', true),
Question('It is illegal to pee in the Ocean in Portugal.', true),
Question('No piece of square dry paper can be folded in half more than 7 times.', false),
Question('In London, UK, if you happen to die in the House of Parliament, you are technically '
'entitled to a state funeral, because the building is considered too sacred a place.', true),
Question('The loudest sound produced by any animal is 188 decibels. '
'That animal is the African Elephant.', false),
Question('The total surface area of two human lungs is approximately 70 square metres.', true),
Question('Google was originally called "Backrub".', true),
Question('Chocolate affects a dog's heart and nervous system; a few ounces are enough to '
'kill a small dog.', true),
Question('In West Virginia, USA, if you accidentally hit an animal with your car, '
'you are free to take it home to eat.', true),
];
String getTanong(){
return questions[item].tanong;
}
bool getSagot(){
return questions[item].sagot;
}
void next(){
if (item<11){
item++;
}
}
bool isFinished(){
if(item >= 11){
return true;
}
else{
return false;
}
}
void reset(){
if(isFinished()){
item = 0;
}
}
List<Icon> scoreKeeper = [];
skcorrect(){
Icon(
Icons.check,
color: Colors.green ,
);
}
skwrong(){
Icon(
Icons.close,
color: Colors.red,
);
}
}

我试过使用void,在主机上创建另一个记分器,但仍然没有改变。

您在skcorrect和skwrong函数中缺少return

Widget skwrong(){
return Icon(
Icons.close,
color: Colors.red,
);
}

基本上就是在这里添加null

qb.scoreKeeper.add(
qb.skcorrect()
);

最新更新