代码之家  ›  专栏  ›  技术社区  ›  Rajeshwar

为什么这个回调在这个dart方法中显示为空

  •  0
  • Rajeshwar  · 技术社区  · 7 年前

    所以我的flutter应用程序中有这个代码-这里是函数 refreshState 正在被方法调用 foo 它正在传入lambda。但是在调试期间它会说回调为空。我的回调代码没有被执行。

         void refreshState(Function callback)
            {
                if(isAlive) {
                    setState(() {
                        if (callback != null) {
                            callback;
                        }
                    });
                }
            }
    

    在我的代码中有一点我正在做这个

     void didPush() {
            foo();
        }
    
    void foo()
        {
            refreshState(() {        //<------------------This lambda is showing up as null in the paramter of refreshState
                   isBusy = true;
           });
    }
    

    任何关于为什么这个lamda在 刷新状态 函数参数?

    1 回复  |  直到 7 年前
        1
  •  1
  •   creativecreatorormaybenot    7 年前

    您误解了这里的调试视图。它是一个函数 () 返回( => ) null . 你只是不执行而已。

    () => ...
    

    这只是一条捷径:

    () {
      return ...
    }
    

    执行你的 callback 不过,你需要加上偏执狂。那就是:

    setState(() {
      if (callback != null)
        callback();
    });