Provider: Explain Consumer performance optimizations.

Created on 24 Jul 2019  路  12Comments  路  Source: rrousselGit/provider

The README.md docs say Consumer can be used for performance optimizations, but don't explain what are these optimizations. I think it refers to using the child property, but I only learned about it in other tutorials. I think the docs should be improved from a beginner's perspective.

Most helpful comment

It will be nice to have an example at the docs, showing who will get rebuild, as the technical description can be ambiguous for begginers.

All 12 comments

In fact I still don't understand what's the different between using Consumer vs. without Consumer.

As far as I understood, Consumer can provide a child along with the value. This child is a Widget tree that you made before and gave it to Consumer. This Widget tree is supposed to be immutable, so you only create it once, and reuse it, so if the value is updated only the Widgets that need to be updated are reconstructed, and you inject this child Widget tree to rebuild your tree more efficiently.

I think an example will be more clear that my explanation, hence the request for better documentations.

Also, there may be other differences that I am not aware of.

Ah, actually I explained that in a recent talk.
I'll add it to the documentation soon.

TL;DR: Consumer vs Provider.of changes the widget that rebuilds.

With Provider.of, it's the widget that called that method which rebuilds. With Consumer, it's the Consumer.

That combined with the optional child allows for more gralunar rebuild of the widget tree.

When you say With Provider.of, it's the widget that called that method which rebuilds, you mean that the Widget that we are coding the build method, the one that we got context from, will be rebuild, right? And not the Widget that we placed Provider.of inside, like inside a RaisedButton onPressed method in the middle of the build method.

you mean that the Widget that we are coding the build method, the one that we got context from, will be rebuild, right?

Yes. It's the widget associated with the BuildContext passed to Provider.of that will rebuild when the value changes.

We could technically use Builder instead of Consumer. It'd do the same thing.

It will be nice to have an example at the docs, showing who will get rebuild, as the technical description can be ambiguous for begginers.

https://github.com/rrousselGit/provider/pull/176

I opened a PR that improves the documentation on Consumer. Is that enough?

I read it, but I am on my phone walking, I think the only thing missing is the explanation you gave here on who's going to be updated when there's a new value. I think I didn't find it there, but maybe I just missed it.

Also, does it speaks about dispose?

Also, does it speaks about dispose?

What dispose?

The dispose method for when the provider goes away and we can destroy our resources.

Oh sorry, that's on the provider, here we are at the other end, the consumer, my bad, I am trying to walk and contribute at the same time 馃槄

One thing more I would add is an article summing it all, as reading class documentations only help if you already knows how the library works and you know which classes are you looking for.

Was this page helpful?
0 / 5 - 0 ratings