【问题标题】:Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe Navigator.push after migration to null safety未处理的异常:在迁移到空安全后查找已停用小部件的祖先是不安全的 Navigator.push
【发布时间】:2021-12-10 21:05:12
【问题描述】:

我已将项目的所有包迁移到 null 安全性。

由于一些人认为我必须在 Android (Kotlin) 上处理,扩展第三方库。 现在这样做之后,我收到以下错误:

E/flutter (14210): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (14210): At this point the state of the widget's element tree is no longer stable.
E/flutter (14210): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter (14210): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure>
package:flutter/…/widgets/framework.dart:3944
E/flutter (14210): #1      Element._debugCheckStateIsActiveForAncestorLookup
package:flutter/…/widgets/framework.dart:3958
E/flutter (14210): #2      Element.findAncestorStateOfType
package:flutter/…/widgets/framework.dart:4005
E/flutter (14210): #3      Navigator.of
package:flutter/…/widgets/navigator.dart:2727
E/flutter (14210): #4      _PinCodeScreenState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:vista/…/pin_code/pin_code.dart:274
E/flutter (14210): #5      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (14210): #6      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (14210): <asynchronous suspension>

我有以下代码:

Navigator.push(context, MaterialPageRoute(builder: (context) => MyScreen(response: _validatioResponse)));

上面的代码被RawMaterialButton小部件onPressed调用

【问题讨论】:

    标签: flutter dart dart-null-safety null-safety


    【解决方案1】:

    试试这个方法

     SchedulerBinding.instance.addPostFrameCallback((_) {
                                  Navigator.pushNamedAndRemoveUntil(
                                      context, '/', (_) => false);
                                });
    
    

    【讨论】:

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