简体   繁体   English

我怎么能解决 flutter 中的那种 stful setState

[英]How could i solve that kind of stful setState in flutter

i am completely cannot explain the issue in writing so i am trying to make it step by step我完全无法以书面形式解释这个问题,所以我正在尝试逐步解决

i have Stfl widget page我有Stfl widget page

i am putting the following in it's initState methood我将以下内容放入它的initState methood

 @override
  void initState(){
    Future.delayed(const Duration(seconds: 10), () {
      setState(() {});
      print('ok i rebuilt the state successfully');
    });
    super.initState();
  }

Now if i change anything in UI before Duration(seconds: 10) which i registered it in previous method, it will successfully update the ui as expected.现在,如果我在之前的方法中注册的Duration(seconds: 10)之前更改了UI中的任何内容,它将按预期成功更新 ui。

Now, out of curiosity, I wanted to know if the previous method remains pending and executed after 10 sec if I exit the page or not, and i did the following现在,出于好奇,我想知道如果我退出页面,之前的方法是否仍然挂起并在 10 秒后执行,我做了以下操作

1- i log in to the page to register the function within initstate 1-我登录页面在initstate中注册initstate

2- i I exited the page before the 10 seconds ran out 2- 我在 10 秒用完之前退出了页面

3- ok now i am in difference page waiting for print('ok i rebuilt the state successfully'); 3- 好的,现在我在不同的页面等待print('ok i rebuilt the state successfully'); to print打印

4- well.. it is printed successfully as expected 4- 嗯..它按预期成功打印

Now i repeat the same steps with some changes like following现在我重复相同的步骤并进行一些更改,例如以下

1- i log in to the page to register the function within initstate 1-我登录页面在initstate中注册initstate

2- i I exited the page before the 10 seconds ran out 2- 我在 10 秒用完之前退出了页面

3- ii log in to the page again before 10 sec is done 3- ii 在 10 秒完成之前再次登录页面

ok now it should print two times.. once for the first registered function and once for second one AND yes it print two time as expected but what i notice if i make changes in ui before 10 sec is done so the UI will never update it self by the first registered function (Although I saw the process was successful in console).好的,现在它应该打印两次。第一次注册 function 一次,第二次注册一次,是的,它按预期打印两次,但我注意到,如果我在 10 秒之前对 ui 进行更改,那么 UI 将永远不会更新它self by 第一个注册的 function(虽然我在console看到这个过程是成功的)。 but it update it self by the second registered function.但它会在第二个注册的 function 时自行更新。

now my question is why it does not update by first registered function. and how could i make it update ui by old pending setState method.现在我的问题是为什么它不通过首次注册的 function 进行更新。我如何才能通过旧的挂起setState方法使其更新 ui。

in the fact it happening either with setState or provider.事实上,它发生在setState或提供者身上。

i need this behavior for many reasons我需要这种行为有很多原因

When you navigate to the page, each time an new instance of the Widget is created.当您导航到该页面时,每次都会创建一个新的 Widget 实例。 So the setState is run against the corresponding widget.所以setState是针对相应的小部件运行的。

Imagine you have 2 of these on the same screen, you don't expect one to update the other one's state. Same thing happens here.假设您在同一个屏幕上有 2 个,您不希望一个更新另一个的 state。同样的事情发生在这里。

Also, I don't know if you're doing the future.delayed to test this.另外,我不知道你是否正在做future.delayed来测试这个。 If you're doing this in your app, I suggest you check the mounted property before setting state, so you prevent setting state on a disposed Widget which can cause issues and exceptions.如果你在你的应用程序中这样做,我建议你在设置 state 之前检查mounted属性,这样你就可以防止在处置的 Widget 上设置 state ,这可能会导致问题和异常。

something like就像是

 Future.delayed(const Duration(seconds: 10), () {
      if (!mounted) return;
      setState(() {});
      print('ok i rebuilt the state successfully');
    });

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

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