1

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 Answers2

2

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 the WidgetRef object and use hooks within the same widget.

  • HookConsumerWidget
  • StatefulHookConsumerWidget
  • HookConsumer
Charles
  • 221
  • 4
  • but when using both a hook functionality and a riverpod one under the scope of a HookConsumerWidget, we still need to add the flutter hooks package separately?? because trying to use useState and ref.watch(someProvider) in the same build method says useState doesn't exist and expects me to import flutter_hooks separately – public static void Main Jul 05 '23 at 15:46
  • 1
    yes, you still need to add `flutter_hooks` to your `pubsepc.yaml` file and import it whenever you use hooks. `hooks_riverpod` does not re-export `flutter_hooks`. – Charles Jul 05 '23 at 15:50
1

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:

class FadeIn extends StatefulWidget {
  const FadeIn({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  State<FadeIn> createState() => _FadeInState();
}

class _FadeInState extends State<FadeIn> with SingleTickerProviderStateMixin {
  late final AnimationController animationController = AnimationController(
    vsync: this,
    duration: const Duration(seconds: 2),
  );

  @override
  void initState() {
    super.initState();
    animationController.forward();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animationController,
      builder: (context, child) {
        return Opacity(
          opacity: animationController.value,
          child: widget.child,
        );
      },
    );
  }
}

Using hooks, the equivalent would be:

class FadeIn extends HookWidget {
  const FadeIn({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  Widget build(BuildContext context) {
    final animationController = useAnimationController(
      duration: const Duration(seconds: 2),
    );

    useEffect(() {
      return null;
    }, const []);

    useAnimation(animationController);

    return Opacity(
      opacity: animationController.value,
      child: child,
    );
  }
}

Note that we use the HookWidget class, which allows you to use hooks inside the build method. But at the same time, we do not have WidgetRef ref as an argument to the build method.

And it is the package hooks_riverpod that gives us the widget HookConsumerWidget that allows us to use hooks in the build method, and also has WidgetRef ref as an argument

class HomeView extends HookConsumerWidget {
  const HomeView({Key? key}): super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // HookConsumerWidget allows using hooks inside the build method
    final state = useState(0);

    // We can also use the ref parameter to listen to providers.
    final counter = ref.watch(counterProvider);
    return Text('$counter');
  }
}

You can read more about it here

Ruble
  • 2,589
  • 3
  • 6
  • 29
  • So if I get it right, the hooks_riverpod lets you do hook stuff and riverpod stuff at the same time without having to instal the two packages separately?, so I can still do full riverpod stuff, and also do full hook stuff like useState, useEffect, all with one package? – public static void Main Jul 05 '23 at 15:16
  • Yes, that's right. If you need hooks, use `hooks_riverpod` and `flutter_hooks`. If no hooks are needed, use only `flutter_riverpod`. – Ruble Jul 05 '23 at 15:50