Vandad Nahavandipoor Profile picture
Feb 5 โ€ข 15 tweets โ€ข 4 min read
Confused about #Provider in #Flutter? Thread to explain some of the most important concepts
๐Ÿงต๐Ÿ‘‡๐Ÿป
context.select() is used when you want to watch a specific aspect of a provider and rebuild your widget upon that value changing. It's only useful inside the build() function of your widgets and changes to the selected value will mark the widget as needing to be rebuilt
by using context.select(), you rebuild your widget only if the given aspect of your provider changes, this is specially useful if you have a large provider with many moving parts
When a provider emits an update, such as with notifyListeners(), all selectors will be called by the Provider library, only if the returned value is different from the previously returned value
context.read(): This function allows you to interact with your provider. You should use read() inside your event handlers and not directly inside the build() function. read() internally uses Provider.of<T>(this, listen: false) meaning that it doesn't listen for changes
context.read(): Use read() mainly to send values to your provider and use select() to read values that might not change
context.watch() as its name indicates depends on a given provider up the hierarchy and marks the widget as needing to be rebuilt if the provider
context.watch() allows you to depend on an optional provider using context.watch<ProviderName?>
context.watch() is the same as Provider.of<T>(context) because listen: true is default in .of<T> function
context.watch() should only be used inside build() functions of StatelessWidget and the build() function of State
If you need context.watch() outside build functions, use Provider.of<T> instead
Consumer creates a new Widget and calls the builder with its own BuildContext and wraps itself around the returned widget from the builder
Consumer is useful if the widget returned from the builder parameter depends on the provider before the context itself has access to it
Consumer has a child widget that doesn't get rebuilt when the provider changes
In Consumer, the "child" gets passed to the builder of the consumer and can be reused regardless of changes to the provider

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Vandad Nahavandipoor

Vandad Nahavandipoor Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @vandadnp

Feb 6
Confused about #StatefulWidget and #StatelessWidget in #Flutter? In this thread I'll explain the most basic concepts.
๐Ÿงต๐Ÿ‘‡๐Ÿป
* StalessWidget comes from Widget > DiagnosticableTree > Diagnosticable. It defines a user interface by returning a Widget in its build function. 1/
In StatefulWidget and StatelessWidget, both, the build function receives a BuildContext using which you can retrieve information from up the build chain in order to complete rendering your widgets. 2/
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(