flutter - 构建 QuizPage(dirty, state: _QuizPageState#307d6): The getter 'length' was called on null
问题描述
我正在开发一个颤振测验。错误:
════════ 小部件库捕获的异常═════════════════════════════␕════════════ ══════════════════ 在构建 QuizPage(脏,状态:_QuizPageState # 307d6)时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
错误https://pastebin.com/8JCG7Ecj https://pastebin.com/s0kvXLr1
几秒钟后应用程序被执行
但是我遇到了一个异常,我知道异常在哪里以及它的含义,但我不知道如何解决这个问题。我希望你们中的一些人可以帮助我。
吉特: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,
),
);
}
}
解决方案
链接中的错误消息说明了其他内容:
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() 中存在错误有关更全面的答案,请查看什么是 NoSuchMethod 错误以及如何修复它?
推荐阅读
- python - 在不持久的任务中完成的模型更改
- python - “flask_bootstrap.Bootstrap 对象”没有属性“load_css”
- mysql - 即使对于简单的查询,Mysql 也总是给出语法错误
- sql - 如何从该表的两列中查找同一表中重复值的不同值
- html - webpack捆绑后css文件名消失
- react-native - React Native 异步存储解析
- flutter - 有没有办法在颤动中更新静态方法
- python - 如何在 multiprocessing.Process 中将未腌制的对象作为参数传递?
- c# - 如何模拟(没有任何模拟框架)其类型参数的特定值的通用函数
- javascript - 使用 React Native 从 Firebase 获取数据