简体   繁体   中英

How to call method in specific widget by provider

Simple question for provider.

I have one screen name is intro.dart , it has 3 widgets in the widget tree. Suppose widget "A", "B" and "C" all are stateLess widget. I'm using 'Provider' package. My question is, I just want to call method of "A" widget while clicked on on button of "C" widget. how can I do by provider or Any other easy trick?

Well, that's impossible or at least wrongly designed. You are trying to violate the widget encapsulation and make them directly dependent.

Depends on what exactly you want to achieve that should do the trick:

  1. Wrap all these A, B, C widgets with the provider (you said you are already using provider, but it doesn't mean you provide anything) intro.dart
  2. In C widget consume the provider and call proper method (which would probably update some state)
  3. A widget also consumes the state so it would be re-rendered automatically

If it doesn't solve your current situation - please update the question with exact problem you are facing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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