【问题标题】:Flutter: Looking up a deactivated widget's ancestor is unsafe with pushNamedAndRemoveUntilFlutter:使用 pushNamedAndRemoveUntil 查找已停用小部件的祖先是不安全的
【发布时间】:2021-07-28 17:19:56
【问题描述】:

这发生在第二行,如果我将其注释掉一切都很好:

login(BuildContext context, bool fromExample, String method) async {
    final loading = new Loading();
    await loading.load(context, 'loads_logging_in'.tr);

switch (method) {
  case "Facebook":
    await authService.facebookSignIn(context);
    break;
  case "Google":
    await authService.googleSignIn(context);
    break;
  case "Apple":
    await authService.appleSignIn(context);
    break;
}

loading.cancel();
if (authService.getUser() == null) {
  return false;
}

Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false); // THIS LINE THROWS THE ERROR
}

即使我只有有那条线,我也会得到完全相同的错误:

======== 小部件库捕获的异常 =================================== ===================== 在最终确定小部件树时引发以下断言:查找 停用的小部件的祖先是不安全的。

此时小部件的元素树的状态不再是 稳定。

要在其 dispose() 方法中安全地引用小部件的祖先,请保存 通过调用引用祖先 小部件的dependOnInheritedWidgetOfExactType() didChangeDependencies() 方法。 抛出异常时,这是堆栈: #0 元素._debugCheckStateIsActiveForAncestorLookup。 (包:flutter/src/widgets/framework.dart:3864:9) #1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3878:6) #2 Element.getElementForInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:3905:12) #3 Provider._inheritedElementOf (package:provider/src/provider.dart:327:34) #4 Provider.of (package:provider/src/provider.dart:284:30) ...

Navigator.of(context).pushNamed('/'); 替换该行可以解决问题

【问题讨论】:

    标签: flutter


    【解决方案1】:

    该错误与我重定向到的默认页面上有此错误的提供商之一有关:

    @override
    void dispose() {
         Provider.of<AdState>(context, listen: false).getInterstitial()?.dispose();
         super.dispose();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2021-06-03
      • 2021-04-29
      • 2020-06-19
      • 2021-11-15
      • 2020-12-26
      • 2022-06-26
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多