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
* 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/