颤振Android编程关闭/关闭ProgressHUD对话框,无需按

颤振Android编程关闭/关闭ProgressHUD对话框,无需按,android,ios,flutter,Android,Ios,Flutter,我刚进入Android学习flutter。我想问一下ProgressHUD对话框 如何以编程方式关闭/关闭ProgressHUD对话框?因为在文档中,它使用onPress来关闭对话框。我想要的是在不按onPress的情况下以编程方式关闭对话框 多谢各位 下面ProgressHUD对话框链接的库/包: bool _loading = true; @override void initState() { super.initState(); _progre

我刚进入Android学习flutter。我想问一下ProgressHUD对话框

如何以编程方式关闭/关闭ProgressHUD对话框?因为在文档中,它使用onPress来关闭对话框。我想要的是在不按onPress的情况下以编程方式关闭对话框

多谢各位

下面ProgressHUD对话框链接的库/包:

    bool _loading = true;

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

    _progressHUD = new ProgressHUD(
      backgroundColor: Colors.black12,
      color: Colors.white,
      containerColor: Colors.blue,
      borderRadius: 5.0,
      text: 'Loading...',
    );

    //If I put in this line, there have an error, 
    //the state is null, the dismiss() function called on null.
    _progressHUD.state.dismiss();

  }
_progressHUD.state.dismiss();

下面的代码是启动对话框的代码:

    bool _loading = true;

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

    _progressHUD = new ProgressHUD(
      backgroundColor: Colors.black12,
      color: Colors.white,
      containerColor: Colors.blue,
      borderRadius: 5.0,
      text: 'Loading...',
    );

    //If I put in this line, there have an error, 
    //the state is null, the dismiss() function called on null.
    _progressHUD.state.dismiss();

  }
_progressHUD.state.dismiss();
您可以看到我是否将
\u progressHUD.state.disclose()放入
在函数
initState()
中有一个错误,即状态为null,dismise()函数在null引用上调用

下面的代码用于关闭对话框:

    bool _loading = true;

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

    _progressHUD = new ProgressHUD(
      backgroundColor: Colors.black12,
      color: Colors.white,
      containerColor: Colors.blue,
      borderRadius: 5.0,
      text: 'Loading...',
    );

    //If I put in this line, there have an error, 
    //the state is null, the dismiss() function called on null.
    _progressHUD.state.dismiss();

  }
_progressHUD.state.dismiss();

当小部件未完全构建时,您将忽略init状态下的进度。因此,状态null错误正在显示。若要在init state内部或在未完全设置状态时使用它,可以使用postframe回调

        WidgetsBinding.instance.addPostFrameCallback((_) {
          _progressHUD.state.dismiss();
        });