flutter-error-logging-conventions
Skill1 starsChanged 25 days agoBSD-3-ClauseView on GitHub
Enforces consistent error handling and logging patterns in Flutter apps. Use when reviewing catch blocks, adding error handling, or fixing silent failures. Prevents the catch (_) anti-pattern.
In this repository52 other files it gives its agents.
- academic-research-assistantSkill
- answer-sheetSkill
- app-graphicsSkill
- app-landing-pageSkill
- app-screenshotsSkill
- break-uiSkill
- consumer-app-from-behaviorSkill
- cross-language-safetySkill
- dart-tool-scriptsSkill
- decision-sweepSkill
- engineering-diarySkill
- figma-review-loopSkill
- flutter-app-size-auditSkill
- flutter-async-at-the-edgeSkill
- flutter-async-cubit-safetySkill
- flutter-bloc-clean-architectureSkill
- flutter-clean-architecture-tddSkill
- flutter-cubit-smart-cachingSkill
- flutter-observability-instrumentationSkill
- flutter-paywall-and-review-timingSkill
- flutter-precommit-cleanupSkill
- flutter-router-decisionSkill
- flutter-runtime-safetySkill
- flutter-shorebird-otaSkill
- flutter-stream-architectureSkill
- flutter-strict-analysisSkill
- flutter-web-first-paintSkill
- forced-pivot-15-daySkill
- gplay-consoleSkill
- ios-distribution-feature-flagsSkill
- launch-readinessSkill
- launch-watchSkill
- locale-expert-reviewSkill
- market-pulseSkill
- mobile-ui-referenceSkill
- moscow-prioritizationSkill
- orchestrate-agentic-engineeringSkill
- parallel-agent-inboxSkill
- paywall-conversion-flowSkill
- paywall-transparencySkill
- proactive-crash-repair-loopSkill
- promise-auditSkill
- pr-preview-shippingSkill
- ship-first-dollar-appSkill
- slim-ios-simulator-fleetSkill
- spec-sql-harnessSkill
- store-launch-mastermindSkill
- sync-contextSkill
- task-blitzSkill
- techdebtSkill
- vendor-decisionSkill
- web-interface-reviewSkill
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 1 is distinctive, and 7 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.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Ensure every error is captured, logged with context, and never silently swallowed.
- Very specific: line 16
Never use
catch (_): Always capture the exception and log it. - Very specific: line 115
When calling async methods from widget initState():
- Very specific: line 119
Check mounted before calling setState after the await
- Very specific: line 140
Check mounted before setState after every await, not only in initState paths.
- Very specific: line 141
Use context.mounted before Navigator, ScaffoldMessenger, showDialog or any other use of a BuildContext after an await.
- Very specific: line 144
Do not wrap setState in a try/catch to silence "setState() called after dispose()".
- Very specific: line 147
Prefer cancelling the timer, animation listener or stream subscription in dispose(), which the framework's own error text calls "the …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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 Error Logging Conventions
Ensure every error is captured, logged with context, and never silently swallowed.
The Rule
Never use catch (_). Always capture the exception and log it.
// BAD - silent failure, impossible to debug
catch (_) { return null; }
// GOOD - logged with service context
catch (e) {
debugPrint('PRODUCT_SERVICE: Failed to fetch products: $e');
return null;
}Logging Format
PREFIX: Operation description: $e- PREFIX: Uppercase service/class name for easy log filtering
- Operation: What was being attempted
- $e: The exception
Examples
debugPrint('USER_SERVICE: Failed to load profile: $e');
debugPrint('CART_SERVICE: Failed to persist cart: $e');
debugPrint('SETTINGS_SECTION: Settings load failed: $e');
debugPrint('UPLOAD_SERVICE: Photo permission denied: $e');
debugPrint('SEARCH_CUBIT: Failed to save recent searches: $e');Common Prefixes
| Layer | Prefix Pattern | Example |
| Services | {SERVICE_NAME}_SERVICE | USER_SERVICE, CART_SERVICE |
| Cubits | {FEATURE}_CUBIT | SEARCH_CUBIT, PROFILE_CUBIT |
| Widgets | {WIDGET_NAME} | SETTINGS_SECTION, ORDER_SUMMARY |
| Repositories | {FEATURE}_REPO | ITEM_REPO, PROFILE_REPO |
Patterns
Service method that serves cache on failure
Future<List<Product>?> fetchProducts() async {
try {
return await _api.fetchProducts(category);
} catch (e) {
debugPrint('PRODUCT_SERVICE: Failed to fetch products: $e');
return _getCachedProducts(); // Serve cached copy instead
}
}Widget async init
Future<void> _initService() async {
try {
await _service.init();
if (mounted) setState(() {});
} catch (e) {
debugPrint('MY_WIDGET: Service init failed: $e');
}
}Cubit with Either result
final result = await _useCase(params);
result.fold(
(failure) {
debugPrint('FEATURE_CUBIT: Use case failed: ${failure.message}');
emit(FeatureError(failure.message ?? 'Unknown error'));
},
(data) => emit(FeatureLoaded(data: data)),
);Audit Command
Find all catch (_) violations:
grep -rn "catch (_)" lib/Find catch blocks without logging:
grep -rn -A2 "catch (e)" lib/ | grep -B1 "return\|rethrow" | grep -v "debugPrint"Widget Lifecycle Safety
When calling async methods from widget initState():
- Move to a separate
_initX()async method (never await in initState) - Wrap in try/catch with logging
- Check
mountedbefore callingsetStateafter the await
@override
void initState() {
super.initState();
_initSettings(); // Fire-and-forget
}
Future<void> _initSettings() async {
try {
await _service.init();
if (mounted) setState(() {});
} catch (e) {
debugPrint('SETTINGS_SECTION: Settings load failed: $e');
}
}After any await in a widget
- Check
mountedbeforesetStateafter every await, not only ininitStatepaths. - Use
context.mountedbeforeNavigator,ScaffoldMessenger,showDialogor any other use of aBuildContextafter an await. Inside aState, theuse_build_context_synchronouslylint accepts the State'smountedfor the State's owncontext. For aBuildContextpassed in or held in a local variable, it requires that context's ownmounted(dart.dev lint page, read 2026-11-03). - Do not wrap
setStatein a try/catch to silence "setState() called after dispose()". The framework throws that error inside anassert, so it appears only in debug and profile builds. A release build skips it and fails later on_element!.markNeedsBuild(), so a catch hides a real failure (inference from readingframework.darton master, read 2026-11-03). - Prefer cancelling the timer, animation listener or stream subscription in
dispose(), which the framework's own error text calls "the preferred solution".mountedis the fallback for a one-shot Future that cannot be cancelled. - The
mountedcheck stops UI work and does not cancel the request or its side effects, so do not rely on it for a payment or verification call that must finish. One reply on the source post reports a crash in a payment flow when the user left the screen mid verification call (single reply report).
Sources
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.

