didChangeDependencies hook in Flutter Widget includes not accurate data of the class

后端 未结 2 586
执念已碎
执念已碎 2021-01-28 13:10

In my code below, I am struggling with LifeCyrles in Flutter where I can update my State in Provider, APPARENTLY, only in didChangeDependencies hook or in a templat

2条回答
  •  孤独总比滥情好
    2021-01-28 14:14

    At the long run, I seem to have found Mounted lifecycle hook in Flutter which is implemented with the help of Future.microtask. Unlike .addPostFrameCallback:

    SchedulerBinding.instance
            .addPostFrameCallback((_) => this._detectLastPage());
    

    Which is triggered only once like InitState (but only at the end of the build execution), Future.microtask can be placed inside build block and be invoked after every change and state update.

    It doesn't solve the problem with the inaccurate state in didChangeDependencies hook but provides another way to perform post-build executions.

    Credits for the current solution to @Abion47

    example

    Future.microtask(() => this._detectLastPage());
    

提交回复
热议问题