You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter迁移scoreKeeper至QuestionBank后遇Row子项Null值错误

问题解决与OOP优化方案

错误根源

你遇到的Row's children must not contain any null values错误,核心问题是QuestionBank类里的skcorrect()和skwrong()方法没有返回值。这两个方法内部创建了Icon对象,但没通过return语句返回,导致调用它们时实际得到的是null。当这些null被添加到scoreKeeper列表后,Row组件渲染时就会触发空值错误。

修复步骤

1. 修正图标方法的返回值

修改questionbank.dart中的skcorrect()和skwrong(),让它们返回创建好的Icon对象:

Icon skcorrect(){
  return Icon(
    Icons.check,
    color: Colors.green ,
  );
}

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

2. 优化OOP封装(推荐)

为了更贴合OOP思想,建议把“记录得分图标”的逻辑完全封装到QuestionBank中,避免UI层直接操作类内部的列表,让类的职责更清晰:
在QuestionBank中新增一个方法:

void recordAnswer(bool userAnswer) {
  if (userAnswer == getSagot()) {
    scoreKeeper.add(skcorrect());
  } else {
    scoreKeeper.add(skwrong());
  }
  next();
}

然后在main.dart的checkAnswer方法中,替换原有判断逻辑:

else{
  qb.recordAnswer(A);
}

3. 细节优化

  • isFinished()方法可简化为一行:return item >= 11;
  • reset()方法里直接用scoreKeeper.clear()清空列表,比重新赋值更合理。

完整修复后的questionbank.dart

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(){
    return item >= 11;
  }

  void reset(){
    item = 0;
    scoreKeeper.clear();
  }
  
  List<Icon> scoreKeeper = [];
  
  Icon skcorrect(){
    return Icon(
      Icons.check,
      color: Colors.green ,
    );
  }

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

  void recordAnswer(bool userAnswer) {
    if (userAnswer == getSagot()) {
      scoreKeeper.add(skcorrect());
    } else {
      scoreKeeper.add(skwrong());
    }
    next();
  }
}

修复后的checkAnswer方法(main.dart)

void checkAnswer (bool A){
  setState(() {
    if(qb.isFinished()){
      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();
    } else{
      qb.recordAnswer(A);
    }
  });
}

内容的提问来源于stack exchange,提问作者therese

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 16:01:47