![]() While this API may look a little wonky, it's pretty impressive. It is then passed into the builder as it's third argument. So, what's going on here? The child widget passed to Consumer is built on it own, outside of the builder callback. I had to tweak the code, and here's an improved live example: A better way to solve that is by using the Consumer.child argument in tandem with the builder. But, that brings us back to the inconvenience we were trying to solve in the first place (robust, repeated code). The obvious way, I guess, to solve this would be to wrap a consumer around each of the individual widgets that need it. Consumer offers a way to solve this problem, as well. If there are hundreds of widgets in your tree, you might see a performance hit. The entire page is rebuilding every time any widget in the page is rebuilt. ![]() Here, that may be okay because it's:Īnd multiple children of the Scaffold need access to the person instance In the examples so far, you're using the consumer to wrap the entire page. Recall this previous example: class MyHomePage extends StatelessWidget Optimizing Consumer In plain English, Consumer exposes instances of provided models, so you can display data and call methods on your provided model. Thus creating a wonderful union.Ĭonsumer is an object in the Provider library that offers a simple API to interact with your provided models in the widgets themselves. It's also valuable because it allows you to rebuild your UI when your state changes, without any manual work. Half the value of Provider comes from the way it allows you to easily decouple your business logic and inject it into your app.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |