导航到新路线时处理小部件 [英] Dispose widget when navigating to new route

查看:84
本文介绍了导航到新路线时处理小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个屏幕。

I have two screens in my app.

屏幕A在打开时运行计算量大的操作,并在<$时取消对数据库的动画/订阅来正确处理调用c $ c> dispose()可以防止内存泄漏。

Screen A runs a computationally expensive operation while opened, and properly disposes by cancelling animations/subscriptions to the database when dispose() is called to prevent memory leaks.

从屏幕A,您可以打开另一个屏幕(屏幕B)。

From Screen A, you can open another screen (Screen B).

当我使用 Navigator.pushNamed 时,屏幕A保留在内存中,并且 dispose( )不会被调用,即使现在显示了屏幕B。

When I use Navigator.pushNamed, Screen A remains in memory, and dispose() is not called, even though Screen B is now shown.

有没有一种方法可以在屏幕A不在屏幕A时强制处理它

Is there a way to force disposal of Screen A when it is not in view?

从不处理第一条路线的示例代码:

Example code where first route is never disposed:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatefulWidget {
  @override
  _FirstRouteState createState() => _FirstRouteState();
}

class _FirstRouteState extends State<FirstRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: RaisedButton(
        child: Text('Open route'),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SecondRoute()),
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    // Never called
    print("Disposing first route");
    super.dispose();
  }
}

class SecondRoute extends StatefulWidget {
  @override
  _SecondRouteState createState() => _SecondRouteState();
}

class _SecondRouteState extends State<SecondRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: RaisedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: Text('Go back!'),
      ),
    );
  }

  @override
  void dispose() {
    print("Disposing second route");
    super.dispose();
  }
}


推荐答案

I知道有点晚了,但是我认为您应该重写 deactivate 方法。由于我们正在更改页面,因此实际上并没有销毁它,这就是为什么未调用处置的原因。

I know it's a bit late but I think you should override the deactivate method. Since we are changing the page we are not actually destroying it, that's why the dispose isn't being called.

d了解更多信息此页面列出有状态生命周期

If you'd like more information this page lists the lifecycle of the stateful widgets.

从链接中:


'deactivate()'是在从树中删除State时调用的,但可能会在当前帧更改完成之前重新插入
。这种方法基本上存在
,因为状态对象可以从树中的一个点移到另一点。

'deactivate()' is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another.

这篇关于导航到新路线时处理小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆