Complete BLoC with Clean Architecture (group chat) Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Online Learning Course App (Riverpod) Online Learning Course App (Getx) Discount !! Shopping App (Provider) Cool Flutter Game Flutter Nodejs Chat App Flutter Nodejs Api And Firebase Chat App Riverpod Task Management App
Riverpod WidgetRef ref object comes with many handy properties that are useful for accessing the providers and related context.
But passing WidgetRef ref object in constructor dependencies is not a good. Official document does not recommend passing the ref object like.
They say it's
dangerous to pass the ref object with it.
The reason it's dangerous that every WidgetRef object is related to a Widget object. Since ref object is associated with a Widget, then if we pass to a controller directly, it creates a misuse of the object. In fact any one can use it. And this may cause null issue or wrong context.
To understand why, consider that this: passing
WidgetRef is a bad idea because
WidgetRefs are (indeed) linked their
Widget's element. Their life cycle are indeed tied to the life cycle of a widget, whereas the lifecycle of a provider is a different thing. In a certain way,
WidgetRef is closer to an
If you really need to pass the WidgetRef ref object, then you may pass it to function from a Widget.
The appButton widget is inside Center widget and Center widget is inside ConsumerStatefulWidget. So we can access the ref object here.
And this ref object is particularly tied to current app widget and that's very specific. Since it's very specific we can pass it to another function as WidgetRef object.
Thus accessing WidgetRef is much safer now. See how the creator of Riverpod says about it.