在构建QuizPage(dirty,state:_QuizPageState#307d6)时引发了以下NoSuchMethodError:在null上调用了getter'length'

时间:2020-10-07 01:43:59

标签: flutter exception

我正在开发一个轻量级测验。错误:

════════小部件库捕获到异常══════════════════════════════════ ═════════════════════ 在构建QuizPage(脏状态,_QuizPageState#307d6)时引发了以下NoSuchMethodError: getter'length'在null上被调用。 接收者:null 尝试致电:长度

错误https://pastebin.com/8JCG7Ecj https://pastebin.com/s0kvXLr1

几秒钟后执行该应用程序

但是我有一个例外,我知道例外在哪里及其含义,但是我不知道如何解决该问题。我希望你们中的一些人能帮助我。

Git:https://github.com/ivsongborges/app_g6pd.git

class QuizPage extends StatefulWidget {
  @override
  _QuizPageState createState() => _QuizPageState();
}

class _QuizPageState extends State<QuizPage> {
  final _controller = QuizController();
  List<Widget> _scoreKeeper = [];

  bool _loading = true;

  @override
  void initState() {
    super.initState();
    _initialize();
  }

  Future<void> _initialize() async {
    await _controller.initialize();

    setState(() {
      _loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue,
        title: Text('QUIZ G6PD ( ${_scoreKeeper.length}/${_controller.questionsNumber} )'),
        centerTitle: true,
        elevation: 0.0,
      ),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 20.0),
          child: _buildQuiz(),
        ),
      ),
    );
  }

  _buildQuiz() {
    if (_loading) return CenteredCircularProgress();

    if (_controller.questionsNumber == 0)
      return CenteredMessage(
        'Sem questões',
        icon: Icons.warning,
      );

    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        _buildQuestion(_controller.getQuestion()),
        _buildAnswerButton(_controller.getAnswer1()),
        _buildAnswerButton(_controller.getAnswer2()),
        _buildScoreKeeper(),
      ],
    );
  }

  _buildQuestion(String question) {
    return Expanded(
      flex: 5,
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 16.0),
        child: Center(
          child: Text(
            question,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 25.0,
              color: Colors.black,
            ),
          ),
        ),
      ),
    );
  }

  _buildAnswerButton(String answer) {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 8.0),
        child: GestureDetector(
          child: Container(
            padding: EdgeInsets.all(4.0),
            color: Colors.blue,
            child: Center(
              child: AutoSizeText(
                answer,
                maxLines: 2,
                minFontSize: 10.0,
                maxFontSize: 32.0,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.0,
                ),
              ),
            ),
          ),
          onTap: () {
            bool correct = _controller.correctAnswer(answer);

            ResultDialog.show(
              context,
              question: _controller.question,
              correct: correct,
              onNext: () {
                setState(() {
                  _scoreKeeper.add(
                    Icon(
                      correct ? Icons.check : Icons.close,
                      color: correct ? Colors.green : Colors.red,
                    ),
                  );

                  if (_scoreKeeper.length < _controller.questionsNumber) {
                    _controller.nextQuestion();
                  } else {
                    FinishDialog.show(
                        context,
                        hitNumber: _controller.hitNumber,
                        questionNumber:  _controller.questionsNumber
                    );
                  }
                });
              },
            );
          },
        ),
      ),
    );
  }

  _buildScoreKeeper() {
    return Expanded(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: _scoreKeeper,
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

链接中的错误消息指出了其他内容:

    I/flutter (26474): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (26474): The following NoSuchMethodError was thrown building QuizPage(dirty, state: _QuizPageState#d376a):
I/flutter (26474): The getter 'length' was called on null.
I/flutter (26474): Receiver: null
I/flutter (26474): Tried calling: length
I/flutter (26474):
I/flutter (26474): The relevant error-causing widget was:
I/flutter (26474):   QuizPage file:///C:/Users/ivs_g/AndroidStudioProjects/dog/flutter_app/lib/pages/home.page.dart:26:43
I/flutter (26474):
I/flutter (26474): When the exception was thrown, this was the stack:
I/flutter (26474): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter (26474): #1      QuizController.questionsNumber (package:flutter_app/controllers/quiz_controller.dart:13:44)
I/flutter (26474): #2      _QuizPageState.build (package:flutter_app/pages/quiz.page.dart:39:71)
I/flutter (26474): #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
I/flutter (26474): #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
I/flutter (26474): #5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
I/flutter (26474): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
I/flutter (26474): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)

很明显,最后调用的方法是QuizController.questionNumber,因此,问题是_contoller.questionNumber 问题是_controller尚未正确声明。应该是final QuizController _controller = QuizController(); 或QuizController()内部错误 有关更全面的答案,请查看What is a NoSuchMethod error and how do I fix it?