flutter-cubit-smart-caching
Skill1 starsChanged 27 days agoBSD-3-ClauseView on GitHub
Adds time-based smart caching to Flutter BlocCubit data cubits to prevent unnecessary reloads on navigation. Use when adding new cubits, fixing redundant API calls, or optimizing navigation performance in Flutter apps.
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-error-logging-conventionsSkill
- 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
3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 24
Add three things to every data cubit:
- Distinctive: line 76
When adding smart caching to a cubit:
- Very specific: line 93
Not forcing refresh after add/delete/update mutations
≈ “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 Cubit Smart Caching Pattern
Prevent unnecessary data reloads when users navigate between tabs/pages by adding time-based freshness checks to Cubit classes.
The Problem
Flutter apps using BlocProvider at root level keep cubits alive across navigation. But pages often call loadData() in their initState or _EmptyView, triggering a full reload every time the user switches tabs. This causes:
- Unnecessary API/database calls
- Loading spinners on every tab switch
- Wasted bandwidth and battery
The Pattern
Add three things to every data cubit:
1. Freshness Tracking
DateTime? _fetchedAt;
static const _staleAfter = Duration(minutes: 5);2. Skip Logic in Load Method
Future<void> loadData({bool force = false}) async {
// Skip if data is fresh (unless forced)
if (!force && state is DataLoaded && _fetchedAt != null) {
if (DateTime.now().difference(_fetchedAt!) < _staleAfter) {
return;
}
}
// Only show loading spinner on FIRST load
if (state is! DataLoaded) {
emit(const DataLoading());
}
// ... fetch data ...
_fetchedAt = DateTime.now();
emit(DataLoaded(data: result));
}3. Force Refresh on Mutations
Future<void> addItem(Item item) async {
await _repository.add(item);
await loadData(force: true); // Force refresh after mutation
}Key Decisions
| Decision | Choice | Why |
| Refresh interval | 5 min for data, 30 min for expensive or rate-limited calls | Balances freshness vs cost |
| Loading spinner | Only on first load | Users see stale data briefly rather than spinner on every tab |
| Force parameter | Defaults to false | Pages call without force; mutations call with force: true |
| Cubit placement | Root MultiBlocProvider | Cubit persists across navigation, cache survives tab switches |
Checklist
When adding smart caching to a cubit:
- Add
DateTime? _fetchedAtfield - Add
static const _staleAfterwith appropriate duration - Add
bool force = falseparameter to load method - Add freshness check at top of load method
- Only emit Loading state on first load (
state is! DataLoaded) - Set
_fetchedAt = DateTime.now()before emitting loaded state - Add
force: trueto all mutation methods that need immediate refresh - Ensure cubit is in root
MultiBlocProvider(not created per-page)
Anti-Patterns
- Constructing a cubit inside
build(), such asBlocProvider.value(value: DataCubit()), which creates a new instance on every build - Providing a data cubit per page with
BlocProvider(create: ...), which disposes the cubit and its cache when the page leaves the tree - Calling
loadData()unconditionally in every page'sinitState - Showing loading spinner on every navigation (users see flicker)
- Not forcing refresh after add/delete/update mutations
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.

