问题
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