How to call a method from InheritedWidget only once?

删除回忆录丶 提交于 2019-12-23 11:36:09

问题


I'm developing with reactive components streams/observables with Rx_command, Rx_dart

Problem:

  • In my Flutter app I have inherited widget that can be called anywhere with:

    FooProvider.of(context).foo.method1...

  • I need to make a first call to the method when the UI loads at first time

  • I can't use init.state because it's impossible
  • I use didchangedependencies it works but..

    ... every time the ui reloads, the didchangedependencies is called and the method is executed once again.

I don't want it to be executed and I can't use init.state

How can execute the method only once?


回答1:


Instead of context.inheritFromWidgetOfExactType, use context.ancestorInheritedElementForWidgetOfExactType

final myInherited = context.ancestorInheritedElementForWidgetOfExactType(MyInherited)?.widget;

This method is available inside initState



来源:https://stackoverflow.com/questions/53022964/how-to-call-a-method-from-inheritedwidget-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!