简体   繁体   English

警报对话框:查找已停用小部件的祖先是不安全的

[英]Alert Dialogs: Looking up a deactivated widget's ancestor is unsafe

In the code below, I am displaying a dialog box with a loader when the form is submitting, then on Error, dismissing the dialog box by popping from its context.在下面的代码中,我在提交表单时显示一个带有加载程序的对话框,然后在出现错误时,通过从其上下文中弹出来关闭对话框。 However, Flutter is giving me that error as the context is no longer available after popping, but this doesn't break my app because I am recreating a context everytime a dialog box is shown.但是,Flutter 给了我这个错误,因为弹出后上下文不再可用,但这不会破坏我的应用程序,因为每次显示对话框时我都会重新创建上下文。 So, to improve my code, what can I do to fix this error?那么,为了改进我的代码,我能做些什么来修复这个错误呢?

End goal: Display loader on submission, if there is error, stop loader and display toast最终目标:提交时显示加载器,如果有错误,停止加载器并显示 toast

** I know there is other solution like creating a global key for the scaffold, but I am looking for an easy fix here:) ** 我知道还有其他解决方案,例如为脚手架创建全局密钥,但我在这里寻找一个简单的解决方案:)

Widget build(BuildContext context) {
    BuildContext loadingDialogContext;

    return BlocListener<SignUpBloc, SignUpState>(
      listener: (context, state) {
        String message;
        final formStatus = state.formSubmissionStatus;
        if (formStatus is SubmissionFailure) {
          Navigator.of(loadingDialogContext).pop();
          final e = formStatus.exception;
          if (e.code == 'UsernameExistsException' ||
              e.code == 'InvalidParameterException' ||
              e.code == 'InvalidPasswordException' ||
              e.code == 'ResourceNotFoundException') {
            message = e.message;
          } else {
            message = 'An unknown error had occurred. Please try again.';
          }
          toast(
            context: context,
            text: message,
            backgroundColor: kErrorColor,
            textColor: Colors.white,
            iconColor: Colors.white,
          );
        } else if (formStatus is SubmissionInProgress) {
          showDialog(
            context: context,
            builder: (dialogContext) {
              loadingDialogContext = dialogContext;
              return Loader();
            },
            barrierDismissible: false,
          );
        }
      },
      child: Form(
        key: _formKey,
        child: Column(

You can use Scaffold.of :您可以使用Scaffold.of

final scaffoldContext = Scaffold.of(loadingDialogContext).pop();
Navigator.of(loadingDialogContext).pop();
toast(
   context: scaffoldContext,
   text: message,
   backgroundColor: kErrorColor,
   textColor: Colors.white,
   iconColor: Colors.white,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM