flutter - 如何解决 flutter/dart 中的 Late 问题

标签 flutter dart void

我在 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/

相关文章:

azure - 如何从管道 Azure DevOps 访问私有(private)存储库?

Azure DevOps中的Flutter管道

dart - 在 flutter 中从 tab1 切换到 tab3 会出现错误 - 最小 flutter App

复杂的C转换解释

c# - 使用 Moq 实现不接受任何参数并返回 void 的方法

flutter - Dart - 从 map 中的 map 读取键和值

flutter - 使用 Hydrated Bloc 版本 5 及更高版本的持久状态

dart - 如何在 Flutter 中将 PointMode 传递给 Canvas.drawPoints(..)?

c++ - 我如何将 int** 转换为 void**?

flutter - 当我运行 (F5) Flutter Web 应用程序时,chrome 上出现空白屏幕(我使用 VScode)