Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method from InheritedWidget only once?

Tags:

flutter

rxdart

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?

like image 740
Paulo Bruckmann Avatar asked Oct 17 '22 11:10

Paulo Bruckmann


1 Answers

Instead of context.inheritFromWidgetOfExactType, use context.ancestorInheritedElementForWidgetOfExactType

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

This method is available inside initState

like image 96
Rémi Rousselet Avatar answered Oct 21 '22 07:10

Rémi Rousselet