简体   繁体   中英

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

I need to move the scorekeeper from main.dart to the questionbank.dart, however, this error keeps showing up and I don't know where and how am I going to fix it. OOP concept must be applied but I am quite confused where to start and how am I going to do it. I get too overwhelmed.

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,
    );
  }

}

I have tried using void, creating another scorekeeper on the maindart but nothing changed still.

you are missing return in skcorrect and skwrong functions

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

basically you are adding null here

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

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.

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