繁体   English   中英

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

[英]OOP Error Row's children must not contain any null values, but a null value was found at index 0

我需要将记分员从 main.dart 移动到 questionbank.dart,但是,这个错误一直出现,我不知道在哪里以及如何修复它。 必须应用 OOP 概念,但我很困惑从哪里开始以及我将如何去做。 我太不知所措了。

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,在 maindart 上创建另一个记分员,但仍然没有任何改变。

你在 skcorrect 和 skwrong 函数中缺少 return

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

基本上你在这里添加 null

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM