简体   繁体   English

Flutter GetX - 如何管理 controller 删除?

[英]Flutter GetX - How to manage controller deletion?

I have a Flutter app with GetX Controllers.我有一个带有GetX控制器的 Flutter 应用程序。 The app has 6 screens and every screen has its GetxController .该应用程序有 6 个屏幕,每个屏幕都有其GetxController

Screens 1 and 2 are for the login system, while screens 3 to 6 are for the app content.屏幕 1 和 2 用于登录系统,而屏幕 3 至 6 用于应用程序内容。

After logging in the user can go forward and back between screens 3-4-5, but when he reaches screen 6 he can only go to Screen 3 and all the previous stacks must be deleted (so he cannot go back).登录后,用户可以 go 在屏幕 3-4-5 之间前进和后退,但是当他到达屏幕 6 时,他只能 go 到屏幕 3,并且必须删除所有之前的堆栈(因此他不能 go 返回)。

1st problem : if I do a Get.offAll(() => const Screen3()) from the Screen 6, the Controller for Screen3 gets deleted and nothing works anymore. 1st problem :如果我从屏幕 6 执行Get.offAll(() => const Screen3()) ,则 Screen3 的 Controller 将被删除,并且不再起作用。 I workaround (don't know if that word exists: :D) by marking Controller3 as permanent via我通过将Controller3标记为永久性解决方法(不知道该词是否存在::D)

Get.put(Controller3(), permanent: true)

But here comes the但是来了

2nd problem : if the user presses the logout button (that is present only in Screen 3), this time I need the Controller3 to be deleted. 2nd problem :如果用户按下logout按钮(仅出现在屏幕 3 中),这次我需要删除Controller3 This time, calling Get.offAll doesn't delete the controller, nor calling Get.delete<Controller3>() , since it says这一次,调用Get.offAll不会删除 controller,也不会调用Get.delete<Controller3>() ,因为它说

"Controller3" has been marked as permanent, SmartManagement is not authorized to delete it. “Controller3”已被标记为永久,SmartManagement 无权删除它。

I'm stuck in this situation and I really don't know what to do我被困在这种情况下,我真的不知道该怎么办

1st problem: if I do a Get.offAll(() => const Screen3()) from the Screen 6, the Controller for the Screen3 gets deleted and nothing works anymore.第一个问题:如果我从屏幕 6 执行 Get.offAll(() => const Screen3()),则屏幕 3 的 Controller 将被删除,并且不再起作用。

I didn't get the quoted part.我没有得到引用的部分。 When you route from 6 --> 3, the binding mechanism should generate the controller of screen 3 again.当您从 6 --> 3 路由时,绑定机制应该再次生成屏幕 3 的 controller。

By the way you can make it manually from anywhere using with顺便说一下,您可以在任何地方使用 with 手动制作它

 var controller = Get.put(SomeController());
 controller.dispose();

So Getx as you said let us make a GetxController permanent like this:所以Getx正如您所说,让我们像这样永久设置GetxController

Get.put<Controller3>.put(Controller3(), permanent: true);.

you can't delete it normally with:你不能正常删除它:

Get.delete<Controller3>();

But you have the option to delete a controller that is marked with permanent , by forcing its deletion with the force property like this:但是您可以选择删除标记为permanent的 controller,方法是使用force属性强制删除它,如下所示:

Get.delete<Controller3>(force: true);

force Will delete an Instance even if marked as permanent. force即使标记为永久也会删除一个实例。

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

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