【问题标题】:Flutter: Unhandled Exception: Looking up a deactivated widget's ancestor is unsafeFlutter:未处理的异常:查找已停用小部件的祖先是不安全的
【发布时间】:2020-06-19 16:54:38
【问题描述】:

我正在尝试在特定持续时间后导航到某个页面。但是,每当我运行该应用程序时,我都会收到一条错误消息:At this point the state of the widget's element tree is no longer stable. To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

我的代码如下所示:

@override
void initState() {
  super.initState();
  googleSignIn.signInSilently(suppressErrors: false)
    .then((account) async {
      if (account != null) {
        List<Post> posts = await initTimelinePosts(account.id);
        List<String> followingList = await configureTimelineForNoPost(
          account.id
        );
        currentUser =
          User.fromDocument(await usersRef.document(account.id).get());
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => Home(
              posts: posts,
              followingList: followingList,
              savedState: widget.savedState
            )
          )
        );
      } else {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            settings: RouteSettings(name: Auth.route),
            builder: (context) => Auth(savedState: widget.savedState)
          )
        );
      }
    }).catchError((_) async {
      final SharedPreferences prefs = await SharedPreferences.getInstance();
      String userId = prefs.getString('userId');
      if (userId != null && userId.isNotEmpty) {
        DocumentSnapshot doc = await usersRef.document(userId).get();
        currentUser = User.fromDocument(doc);
        List<Post> posts = await initTimelinePosts(userId);
        List<String> followingList = await configureTimelineForNoPost(userId);
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => Home(
              posts: posts,
              followingList: followingList,
              savedState: widget.savedState,
            )
          )
        );
      } else {
        _timer = new Timer(
          Duration(milliseconds: 3000),
          () {
            Navigator.pushReplacement( // the error message points to this line
              context,
              MaterialPageRoute(
                settings: RouteSettings(name: Auth.route),
                builder: (context) => Auth(savedState: widget.savedState)
              )
            );
          }
        );
      }
    });
}

我希望应用程序能够正常运行,但事实并非如此。我该如何解决这个问题?

注意:我真的不知道该写什么了,所以一直抱怨我需要添加更多细节。我相信我已经对描述、标题和代码提供了足够的解释。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我认为问题在于您正在尝试导航到新页面并在 initState() 中替换此页面。由于运行 initState() 方法时未构建小部件树,因此可能会导致错误。

    查看this answer 了解详细说明,查看this 了解类似问题。

    【讨论】:

    • 好的,谢谢,我通过检查上下文是否为空解决了这个问题
    • 我运行了该应用程序,但错误再次出现,虽然,当我添加 if 检查时,它第一次消失了,所以我不知道为什么。另外,我注意到代码执行没有到达那条线,因为我尝试在那里打印但它不起作用所以我想知道为什么它在不应该导航的时候尝试导航
    • 试试this。它可能会有所帮助
    猜你喜欢
    • 2020-08-08
    • 2021-06-03
    • 1970-01-01
    • 2021-04-29
    • 2021-07-28
    • 2020-09-18
    • 2022-01-11
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多