简体   繁体   English

Flutter 导航销毁并杀死页面值

[英]Flutter navigation destroy and kill page values

I have two pages one is A another one is B. A is the main page where I view the list of data that is being stored, B is temporary second page(my custom text editor) where I input different values to be saved then viewed in A.我有两页,一个是 A,另一个是 B。 A 是我查看正在存储的数据列表的主页面,B 是临时第二页(我的自定义文本编辑器),我输入不同的值进行保存然后查看在一个。

when I go to B from A using :当我从 A 去 B 时使用:

Navigator.push(context,MaterialPageRoute(builder: (context) => B_page())); after I type the values of data list and click save it takes me to A using Navigator.of(context).pop();在我输入数据列表的值并单击保存后,它会使用Navigator.of(context).pop();将我带到 A。

But when I click again to create another data list in B the page is not rested or cleaned to the previous initialized data is still there like the page was resumed.但是,当我再次单击以在 B 中创建另一个数据列表时,页面不会停止或清理到先前初始化的数据仍然存在,就像页面已恢复一样。

is there another alternative to Navigator.of(context).pop();是否有另一种替代Navigator.of(context).pop(); I want to destroy and clear the page to be created again when clicked in A.我想在 A 中单击时销毁并清除要再次创建的页面。

For example: the first initialize values like String name = '', nickname = '', company = '';例如:第一个初始化值如String name = '', nickname = '', company = ''; or int index = 1;int index = 1; when changed in a TextField to name = 'Name1', nickname = 'Boss', company = 'SmallDoda';TextField更改为name = 'Name1', nickname = 'Boss', company = 'SmallDoda'; or index = 8;index = 8; when .pop() then navigate to B again the values are the same index = 8;.pop()然后再次导航到 B 时,值是相同的index = 8; instead of index = 1;而不是index = 1; I want to kill the current page to be created again.我想杀死要重新创建的当前页面。

The TextField creates a TextEditingController if you don't supply one yourself.如果您自己不提供,则TextField会创建一个TextEditingController

I'm guessing there's a reference to your B page/screen that is keeping your TextField on that page in memory and not disposing it.我猜有一个对您的 B 页面/屏幕的引用,它将您的TextField保留在内存中的该页面上,而不是处理它。 So when you return to B, the TextField and its TextEditingController are still the same from before, showing the previous value.所以当你回到B的时候, TextField和它的TextEditingController还是和之前一样,显示的是之前的值。

You can manually control the TextEditingController of TextField by supplying your own TextEditingController and clearing its value when you .pop() back to page A.您可以手动控制TextEditingControllerTextField通过提供自己的TextEditingController ,当你清除其值.pop()返回页面A.

Here's another answer showing you exactly how .这是另一个答案,向您展示了.

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

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