using-flutter-hooks
Skill7 starsChanged 8 months agoMITView on GitHub
Manages local UI state and side effects in Flutter using the flutter_hooks package (v0.21.x) with HookWidget, useState, useEffect, useMemoized, useAnimationController, and custom hooks. Use this skill when replacing StatefulWidget boilerplate with hooks, reusing stateful UI logic across widgets, managing controller lifecycles (TextEditingController, AnimationController, ScrollController) without manual dispose, implementing countdown timers or subscriptions in widgets, mixing flutter_hooks with Riverpod via HookConsumerWidget, or any time the words 'flutter hooks', 'useEffect', 'useState', 'HookWidget', or 'flutter_hooks' appear in the request.
In this repository41 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- skill-creatorSkill
- flutter-dbSkill
- flutter-routingSkill
- flutter-state-managementSkill
- configuring-codemagicSkill
- applying-effective-dartSkill
- deploying-with-fastlaneSkill
- integrating-firebaseSkill
- implementing-fl-chartSkill
- integrating-google-adsSkill
- animating-flutter-widgetsSkill
- routing-with-autorouteSkill
- implementing-flutter-blocSkill
- configuring-deep-linksSkill
- persisting-data-with-driftSkill
- building-flutter-production-appsSkill
- generating-flutter-uiSkill
- routing-with-gorouterSkill
- managing-hive-storageSkill
- running-flutter-isolatesSkill
- managing-flutter-provider-stateSkill
- implementing-responsive-layoutsSkill
- implementing-riverpodSkill
- managing-secure-storageSkill
- flutter-setupSkill
- managing-shared-preferencesSkill
- implementing-flutter-social-authSkill
- flutter-testingSkill
- using-fpdartSkill
- generating-freezed-modelsSkill
- configuring-github-actionsSkill
- building-jaspr-web-appsSkill
- testing-with-marionetteSkill
- implementing-openapi-in-dartSkill
- integrating-revenuecatSkill
- integrating-sentrySkill
- developing-serverpod-backendSkill
- building-shadcn-flutter-uiSkill
- integrating-supabaseSkill
- converting-ts-to-dartSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 33
All Hooks are only allowed to be called at the top level inside the build method.
- Distinctive: line 81
If multiple parts in your project need an "obtain current App lifecycle state (AppLifecycleState)", rather than writing a bunch of …
- Very specific: line 122
If you need access to a lower-level BuildContext or want to intercept Flutter's native didUpdateContext, you can create a specific Hook …
- Very specific: line 156
Ensure that you ONLY invoke Hooks at the very top level of a build method.
- Very specific: line 157
Make sure hooks_riverpod is imported if you're mixing Riverpod Providers with flutter hooks.
- Very specific: line 158
Do NOT enforce Controller.dispose() if implementing hooks like useAnimationController().
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Flutter Hooks Latest Version Best Practices Guide (v0.21.x)
Goal
flutter_hooks is a package developed by Remi Rousselet (author of Riverpod and Freezed). Its core philosophy stems from React Hooks. The current latest version is 0.21.3+1.
Hooks primarily exist to solve the verbose boilerplate code brought about by StatefulWidget, and the extreme difficulty of reusing UI Logic across Widgets.
Instructions
1. Core Concept: What are Hooks?
Hooks are tools used to manage "local and ephemeral UI state or side effects".
- Not Suitable For: Global business logic management (this should be handed over to Riverpod or Bloc).
- Suitable For: Any Widget attaching a
Controller, animation management, focus tracking, or countdown timers.
Dependency Installation
dependencies:
flutter_hooks: ^0.21.3
hooks_riverpod: ^3.3.0 # If you are simultaneously using Riverpod2. Widget Declaration: Replacing StatefulWidget
To use Hooks, your Widget cannot inherit from a standard StatelessWidget or StatefulWidget; it must instead inherit from HookWidget.
(If you are mixing it with Riverpod, inherit from HookConsumerWidget instead).
All Hooks are only allowed to be called at the top level inside the build method. Writing them inside if statements is strictly prohibited.
3. Built-in Hooks Overview and Best Practices
flutter_hooks provides a massive number of practical built-in Hooks. Leveraging them effectively can save hundreds of lines of dispose() and initState().
3.1 State and Caching (State & Caching)
useState(initialData):- Purpose: Manages the simplest local state (essentially wraps
ValueNotifier). - Usage:
final counter = useState(0); counter.value++;
- Purpose: Manages the simplest local state (essentially wraps
useMemoized(compute, [keys]):- Purpose: Caches the result of an expensive computation, or ensures an object (e.g., a custom class) is instantiated only once during Rebuilds.
- Usage:
final complexObj = useMemoized(() => HeavyObject(), []);
useCallback(callback, [keys]):- Purpose: Caches a function reference. If passed to deep Widgets, it prevents triggering unnecessary refreshes because parent Rebuilds generate a new function memory location.
useRef(initialValue):- Purpose: Holds a mutable object across Rebuild cycles, and changing its value does not trigger a UI refresh (unlike
useState). Perfect for storing the width of a previous screen, or a Timer instance for subsequent clearing.
- Purpose: Holds a mutable object across Rebuild cycles, and changing its value does not trigger a UI refresh (unlike
3.2 Side Effects and Lifecycle (Side Effects)
useEffect(effect, [keys]):- Purpose: The perfect alternative to
initStateanddispose. Responsible for subscriptions, timers, and any side effect with a start and end point. - Usage:
useEffect(() { print('Equivalent to initState (Triggers only when key changes or upon initialization)'); return () => print('Equivalent to dispose (Executes before Widget destruction or before effect re-triggers)'); }, []); // Passing [] ensures it runs only once- Purpose: The perfect alternative to
useIsMounted():- Purpose: Following an asynchronous request, ensures the Widget has not yet been destroyed before executing operations like
setState. - Usage:
final isMounted = useIsMounted(); ... if(isMounted()) { ... }
- Purpose: Following an asynchronous request, ensures the Widget has not yet been destroyed before executing operations like
3.3 Auto-cleaning Controllers (Most Powerful Feature)
Forget writing controller.dispose() manually! The following Hooks automatically create and destroy them behind the scenes for you:
useTextEditingController(text: 'default value')useAnimationController(duration: ...)useScrollController()usePageController()useTabController(length: 3)useFocusNode()
3.4 Reactive Wrappers (Reactive Streams & Futures)
useFuture(future)/useStream(stream):- Purpose: Minimalist versions of
FutureBuilder/StreamBuilder, returning anAsyncSnapshot.
- Purpose: Minimalist versions of
useListenable(listenable)/useValueListenable(notifier):- Purpose: Allows a Widget to listen to traditional ChangeNotifier or ValueNotifier and automatically trigger a rebuild.
4. Advanced Practices: Custom Hooks (Custom Hooks)
The greatest power of Hooks lies in encapsulating and perfectly reusing UI logic. If multiple parts in your project need an "obtain current App lifecycle state (AppLifecycleState)", rather than writing a bunch of observers on every page, writing it as a Custom Hook solves it once and for all.
Naming Convention: All custom Hooks MUST begin with
use.
Approach A: Composing Multiple Existing Hooks (Composition - Highly Recommended)
The simplest and most favored approach is directly writing a function that returns a result.
// Encapsulates a set of "countdown timer" logic
int useCountdown({required int seconds}) {
final timeLeft = useState(seconds);
final isMounted = useIsMounted();
useEffect(() {
final timer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (!isMounted()) {
timer.cancel();
return;
}
if (timeLeft.value > 0) {
timeLeft.value--;
} else {
timer.cancel();
}
});
return timer.cancel; // Ensures it definitely gets closed
}, [seconds]); // Resets the timer when 'seconds' change
return timeLeft.value;
}
// Used in a Widget like this:
Widget build(BuildContext context) {
final count = useCountdown(seconds: 30);
return Text("Remaining $count seconds");
}Approach B: Extending the Hook Class Definition (Advanced / Low-level Library Development)
If you need access to a lower-level BuildContext or want to intercept Flutter's native didUpdateContext, you can create a specific Hook much like crafting a StatefulWidget.
// This pattern is typically geared towards third-party package developers
int useMyComplexLogic() => use(const _MyComplexHook());
class _MyComplexHook extends Hook<int> {
const _MyComplexHook();
@override
_MyComplexHookState createState() => _MyComplexHookState();
}
class _MyComplexHookState extends HookState<int, _MyComplexHook> {
int value = 0;
@override
void initHook() { /* Initialization */ }
@override
int build(BuildContext context) => value;
@override
void dispose() { /* Resource cleanup */ }
}5. Conclusion: Integration with Riverpod (The God-tier Combination)
flutter_hooks is not designed to replace BLoC or Riverpod. The optimal modern Flutter development architecture:
- Global Business Logic and Shared State →
Riverpod(riverpod_generator) - Complex In-Page UI Control and Animation →
flutter_hooks(useAnimationController,useTextEditingController)
By combining the two, your project will practically contain zero StatefulWidgets. All Widgets can remain clean, focusing purely on their primary duty (drawing the screen), thereby yielding the highest quality Flutter applications.
Constraints
- Ensure that you ONLY invoke Hooks at the very top level of a
buildmethod. - Make sure
hooks_riverpodis imported if you're mixing Riverpod Providers with flutter hooks; do not mixhooks_riverpodimports concurrently with rawflutter_riverpod. - Do NOT enforce
Controller.dispose()if implementing hooks likeuseAnimationController().
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

