我在 flutter 中有这段代码
late String firstHalf;
late String secondHalf;
bool hiddenText = true;
double textHeight = Diamension.screenHeight / 5.63;
@override
void iniState() {
super.initState();
if (widget.text.length > textHeight) {
firstHalf = widget.text.substring(0, textHeight.toInt());
secondHalf =
widget.text.substring(textHeight.toInt() + 1, widget.text.length);
} else {
firstHalf = widget.text;
secondHalf = "";
}
}
从代码中你会看到我初始化了“secondHalf”,但我在调试中不断收到此错误
Exception has occurred.
LateError (LateInitializationError: Field 'secondHalf' has not been initialized.)
最佳答案
除非您将错误复制粘贴到 StackOverflow 中,否则您只是错过了一个t
...
尝试将 void iniState() {
更改为 void initState() {
您可能会收到此警告(@julemand101 也指出了这一点):
The method doesn't override an inherited method. Try updating this class to match the superclass, or removing the override annotation.dartoverride_on_non_overriding_member
关于flutter - 如何解决 flutter/dart 中的 Late 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73442563/