【问题标题】:Functions don't get executed form initState()函数不会从 initState() 中执行
【发布时间】:2021-04-22 12:02:59
【问题描述】:

我一直在学习处理颤振中的async 函数。我已经定义了一个async 函数,并在两个地方调用了它
1。来自build 函数。
2。来自initState
当从build 函数调用时,函数会被执行并将文本打印到终端,但是,从initstate 调用它时,终端不会记录任何文本。
这是代码。

import 'package:flutter/material.dart';

void main() => runApp(Test());

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  void initState() {
    random();
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // random();
    return MaterialApp(home: Scaffold(body: Center(child: Text("Wattup"))));
  }

  Future<void> random() async {
    await Future.delayed(Duration(seconds: 2));
    print("I'm Back baby");
  }
}

【问题讨论】:

  • 虽然通常应该首先调用super.initState(),但您的代码应该可以工作-我的意思是从initState调用random()-我刚刚在一分钟前验证过-@987654333之后的print() @ 被调用,我在日志输出中看到它
  • 我 99% 确定您已将 random() 添加到 initState() 并按下“热重载”按钮,对吗?
  • 基本上如果你对现有的StatefulWidget 进行热重载initState 不会被调用,但是如果你热重启它会被调用并且你的random() 方法也会被调用
  • 更多:flutter.dev/docs/development/tools/… - 搜索What is the difference between hot reload, hot restart, and full restart? - 例如:“热重载将代码更改加载到 VM 并重新构建小部件树,保留应用程序状态; 它不会重新运行 main() 或 initState()。(在 Intellij 和 Android Studio 中为⌘\,在 VSCode 中为 ⌃F5)"
  • 正如我所说,如果你在 super.initState() 之前或之后添加它,你的函数应该可以工作,但“正常”的方法是先调用 super 东西,然后是你的额外代码

标签: flutter dart


【解决方案1】:

如果你想在 2 秒后打印,那么你的随机函数应该是这样的,

 Future<void> random() async {
    await Future.delayed(Duration(seconds: 2),(){
      print("I'm Back baby");
    });
  }

这将在 2 秒后打印该行,不管你把它放在 super.init 之前还是之后

【讨论】:

    【解决方案2】:

    尝试在构建小部件后执行该函数,如下所示:

        @override
        void initState () {
          WidgetsBinding.instance.addPostFrameCallback((_){
            random();
          });
          super.initState();
         
        }
    

    或者

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

    【讨论】:

      【解决方案3】:

      initState 方法:

      如果您覆盖它,请确保您的方法开始调用 super.initState()。

      强调我的。

      所以无论您做什么,都需要在您致电super.initState();

      之后进行

      【讨论】:

      • 您能否解释一下在 super.initState 引擎盖视图之前调用它时会发生什么?
      • 坏事。糟糕到他们在文档中对此进行了注释。一般来说,框架设计者知道应该如何对超调用进行排序,因为有时超类要求子类首先采取行动,有时则相反。
      【解决方案4】:

      你需要像这样在super.initState 之后调用你的方法:

        @override
        void initState() {
          // TODO: implement initState
          super.initState();
          random();
      
        }
      

      【讨论】:

      • 您能否解释一下在 super.initState 引擎盖视图之前调用它时会发生什么?
      • initstate 在此对象插入树时调用。如果您不先调用 super.initState,则不会执行任何操作。在这里查看更多详细信息:api.flutter.dev/flutter/widgets/State/initState.html
      • 实际上,在这种情况下,super.initState() 可以首先调用/在中间/最后调用 - 无论你在哪里调用它 - 好的,正常的做法是先调用它 - 更多信息请参阅State.initState 实际上在做什么
      【解决方案5】:

      我认为,应该首先调用 Super.initState() 所以,把它放在第一行。试试这个。

      【讨论】:

      • 您能否解释一下在 super.initState 引擎盖视图之前调用它时会发生什么?
      • 当我们重写任何方法时,这意味着在父类方法中定义了,我们只是在其中添加了更多的东西,所以当 super.initState() 时。它执行父类的initState方法,该方法完成所有主要工作,因此必须先调用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2011-11-09
      相关资源
      最近更新 更多