I’m kinda confused as to what exactly the aim is with hooks_riverpod, I’ve used flutter_riverpod, and I’ve used flutter_hooks, but what is hooks_riverpod, on the pub.dev main page for hooks_riverpod, it’s documentation is just flutter_riverpod documentation, there’s not a single mention of hooks, and it’s basically the same syntaxes and functionality?, I’m super confused, is this a troll package?, when I import hooks_riverpod into my project and remove flutter_riverpod, there are no errors, it just works with my former flutter_riverpod code. How and what is hooks riverpod and where is the documentation, how does it do anything different than flutter_riverpod since flutter riverpod basically even has full hook capabilities with extra features, what’s the point of this combination and where is the evidence of the hooks added to the flutter_riverpod
2
Answers
The
flutter_hooks
package allows you to use hooks like:useEffect
,useState
,useAnimation
,useTextEditingController
and others. The full list is presented here Existing hooks.As an example, we could use hooks to manually implement a fade-in animation, where a widget starts invisible and slowly appears.
If we were to use StatefulWidget, the code would look like this:
Using hooks, the equivalent would be:
Note that we use the
HookWidget
class, which allows you to use hooks inside thebuild
method. But at the same time, we do not haveWidgetRef ref
as an argument to thebuild
method.And it is the package
hooks_riverpod
that gives us the widgetHookConsumerWidget
that allows us to use hooks in thebuild
method, and also hasWidgetRef ref
as an argumentYou can read more about it here
or here in new documentation
You can always check the source code of an open source package.
It is basically a re-export of
flutter_riverpod
with 3 additional classes which allow you to access theWidgetRef
object and use hooks within the same widget.