5

I'm fighting with Flutter's provider and can't understand all of it's possibilities. The thing is that for example I have such widgets structure:

Widget_A
--Widget_B
----Widget_C
----Widget_D
--Widget_E
--Widget_F

Simple example is TODO list. Imagine you have categories, add_field and todos_list. Each is a separate widget. Than you change categories so todos_list should be reloaded to show todos from selected category. Later you add todo using add_field so todos_list also need to be updated because of new record. How can I achieve something like this?

PS: I was trying to separate all the stuff to the different Provider classes, but effect is the same: if widget uses Provider.of(context) it will be updated no matter what. And as I should combine data from 2 providers there will be widget that is connected to the both providers so the result will be endless loop. Still.

Alexander
  • 357
  • 4
  • 20
  • The fact that you have an endless loop of update, added to the fact that you're asking this question, shows that you're doing something anti pattern. – Rémi Rousselet Oct 23 '19 at 22:35
  • Which anti pattern? Data flow isn't so easy. One of the widgets should be updated if 2 other widgets updates... May be there's some widget structure problem, but I don't see it. – Alexander Oct 24 '19 at 06:59
  • Your dataflow is the problem. It should be a unidirectional dataflow. – Rémi Rousselet Oct 24 '19 at 07:26
  • What do you mean? It's kind of unidirectional. But as I understand Provider doesn't care about direction. If widget uses selected Provider it will be updated... Right? At least a see something like this – Alexander Oct 24 '19 at 09:35
  • This is probably not the only problem that you have, nor is the example used strictly identical to yours, but you may want to read https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build. – Rémi Rousselet Oct 24 '19 at 09:38
  • Thank, you. I'll try to separate provider into few different providers and modify logic. But still... Example is todo list. Imagine you have ```categories```, ```add_field``` and ```todos_list```. Each is a separate widget. Than you change ```categories``` so ```todos_list``` should be reloaded, later you add todo using ```add_field``` so ```todos_list``` also need to be updated. How can I achieve something like this? – Alexander Oct 24 '19 at 09:52
  • It depends on your model and use-case, but you should be able to use `Selector`. – Rémi Rousselet Oct 24 '19 at 09:58
  • please, extend your answer. What do you mean by depends on model and use-case – Alexander Oct 24 '19 at 10:22

0 Answers0