
Minimalist Guide to Flutter Hooks
Maximize your code reuse with Flutter Hooks
What is Hooks?
Hooks are a new kind of object that manages a Widget life-cycles. They exist for one reason: increase the code-sharing between widgets by removing duplicates.
In this article, I’ll show you how to reduce your boilerplate code using flutter_hooks.
Why this package exists?
StatefulWidget suffers from a big problem: it is very difficult to reuse the logic of say initState or dispose.
The most exciting thing about this package. when you use hooks, you don’t need to worry about like initState and dispose methods in your widget lifecycle. the package will handle them like a boss instead of you.
If you used any controller before, you understand better the pain we suffer.
Motivation

Examples
I think It will be more effective If I explain this package with examples.
Before I start to explain, don’t forget this package has lots of hooks out of the box. (List of Existing Hooks) I will explain only the most common hooks to make you to better understand the concept. If none of them meet your needs, you can make your own hook, but in this article, I won’t cover that to don’t make things complicated. If you’d like to learn custom hooks please let me know in the comment section
1. State vs useState
HookWidget is an alternative to StatefulWidget. So you don't need to use StatefulWidget’s boilerplate code. Just make your StatelessWidget to HookWidget and that's it. You’re ready to use hooks.
If you want to use state in your hook, just use useState method in the build method.
like this:
class ClassName extends HookWidget { // change this
@override
Widget build(BuildContext context) {
final stateName = useState(initialValue); // add this
print(stateName.value); // example usage
...
}
}You can use all kinds of hook like this.





