A CLAUDE.md asks Claude Code to behave. A hook makes something happen every time, whether or not Claude remembers. For a Flutter project, the useful ones are small: keep Dart files formatted, stop edits to generated or secret files and run a check at the right moment. This page shows how hooks work and gives three examples you can adapt. It uses Claude Code’s hooks documentation, read on the day this post was written. Check that page for the current format, since the configuration has more options than these sketches show.
What a hook is
Claude Code’s documentation describes hooks as “user-defined shell commands, HTTP endpoints, MCP tool calls, LLM prompts, or subagents that execute automatically at specific points in Claude Code’s lifecycle.” The events include PreToolUse, which runs before a tool call and can block it, PostToolUse, which runs after one succeeds, Notification, Stop and SessionStart, among others.
Hooks communicate through standard input and output. When an event fires, Claude Code passes event data as JSON to your script’s standard input. A PreToolUse hook for a file edit receives fields such as tool_name and tool_input, whose file_path holds the file being edited. Your script reads that, does its work and answers with an exit code: the documentation’s example uses exit code 2 to block the action, with the message written to standard error becoming feedback to Claude, and exit code 0 to let the normal permission flow continue.
Where hooks live
From the documentation:
| Location | Scope | Shareable |
|---|---|---|
~/.claude/settings.json |
All your projects | No, local to your machine |
.claude/settings.json |
A single project | Yes, can be committed to the repo |
.claude/settings.local.json |
A single project | No, gitignored |
A plugin’s hooks/hooks.json |
When the plugin is enabled | Yes |
| Skill or subagent frontmatter | While it runs | Yes |
For a Flutter project, put team rules in .claude/settings.json and commit them, so everyone, and every Claude Code session, gets them. Type /hooks in Claude Code to browse what is configured.
Example 1: format Dart after every edit
The documentation shows the pattern for Prettier, a PostToolUse hook with an Edit|Write matcher that reads the edited file’s path with jq. Adapted for Dart:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path') && case \"$f\" in *.dart) dart format \"$f\" ;; esac"
}
]
}
]
}
}
It runs only after file-editing tools, and formats only .dart files. It needs jq installed. When it succeeds Claude Code shows nothing, so check that a file was reformatted to see it working.
Example 2: protect generated and secret files
The documentation’s protected-files example uses a script that checks the target path against a list and exits with code 2 to block the edit. For Flutter, the files to protect are the generated ones, which should be regenerated and never edited by hand, and the secrets:
#!/bin/bash
# .claude/hooks/protect-files.sh
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
PROTECTED=(".g.dart" ".freezed.dart" "key.properties" ".jks" ".env" ".git/")
for pattern in "${PROTECTED[@]}"; do
if [[ "$FILE_PATH" == *"$pattern"* ]]; then
echo "Blocked: $FILE_PATH matches protected pattern '$pattern'" >&2
exit 2
fi
done
exit 0
Register it as a PreToolUse hook with the same Edit|Write matcher, pointing at the script with "$CLAUDE_PROJECT_DIR"/.claude/hooks/protect-files.sh, and make the script executable with chmod +x. Claude receives the “Blocked” message as feedback and can adjust, for example by changing the source and re-running code generation instead.
The limit, from the documentation: “Claude can still change protected files with shell commands.” A file-edit hook does not cover a sed command or a redirect. To block those, add permission deny rules for the paths and consider sandboxing. Use the hook as a guard rail, not a vault. And keep real secrets out of the project folder altogether.
Example 3: a reminder before you stop
A Stop hook runs when Claude finishes responding, which makes it a place to run your checks, flutter analyze and a quick test run. Read the hooks documentation for how a Stop hook reports a result back and for the limit on how many times one can block, because the documentation’s troubleshooting section covers a Stop hook hitting its block cap. Keep the check fast, make it succeed when the project is clean and test it on a small change first. This is the hook to add last, because a slow or flaky check on every stop gets in the way.
Rules for good hooks
- Keep them fast. A hook that takes ten seconds on every edit becomes a tax.
- Make them quiet on success.
- Fail with a clear message on stderr, since that is what Claude reads.
- Be specific with matchers. Run on the tools and files that matter.
- Commit them, so the team gets the same rules, and review changes to them like code.
- Test them by trying to break the rule, not just by running the happy path.
- Do not put secrets in them.
What hooks are not for
Hooks are for deterministic checks. They are not a place to encode judgment, such as “write good tests”. For rules that need reasoning, use a subagent that reviews changes: see a Claude Code subagent as a code reviewer. For standing instructions, use CLAUDE.md: see CLAUDE.md for Flutter. And for a procedure you want on demand, use a skill: see how to write a Claude Code skill.
Where this fits
The generated-file rule matters most with code generation, such as Riverpod’s: see Riverpod with Claude Code. The three “done” commands a hook can run are in how to test a Flutter app with Claude Code. The whole route is in Build a Flutter app with Claude Code.
Sources read on the day this post was written: Claude Code: hooks and Claude Code: automate actions with hooks.