簡體   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