首页 > 解决方案 > 是否有任何解决方案可以解决此错误尝试调用:findAncestorStateOfType()?

问题描述

我试图通过使用加载对话框导航到另一个页面。

 waiting(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return WillPopScope(
          onWillPop: () async => false,
          child: AlertDialog(
              key: _keyLoader,
              content: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    child: CircularProgressIndicator(),
                  ),
                  Container(
                    child: Text("Loading..."),
                  )
                ],
              )),
        );
      },
    );
  }

上面的代码是我的加载对话框。当调用 onPressed 函数时,我想导航到另一个页面。当调用 onPressed 函数时,调用下面的方法。

void _validateInputs(BuildContext context) {
    waiting(context);
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
       setState(() {
        _autoValidate = false;
      });
      
       Navigator.of(_keyLoader.currentContext).pop();
       Navigator.of(context)
                        .push(MaterialPageRoute(builder: (context) {
                      return CareerDetailsPage();
                    }));
     

      _formKey.currentState.reset();
    } else {
      setState(() {
        _autoValidate = true;
      });
    }
  }

我想弹出这个加载对话框并导航到另一个页面。但我得到了以下类型的错误。

The following NoSuchMethodError was thrown while handling a gesture:
The method 'findAncestorStateOfType' was called on null.
Receiver: null
Tried calling: findAncestorStateOfType<NavigatorState>()

你能帮我么?

标签: flutterdart

解决方案


推荐阅读