agentleFS
mikezamayiasmikezamayias/agent-skills/skills/flutter-cubit-smart-caching/SKILL.md

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.

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
  1. Distinctive:

    Add three things to every data cubit:

    line 24
  2. Distinctive:

    When adding smart caching to a cubit:

    line 76
  3. Very specific:

    Not forcing refresh after add/delete/update mutations

    line 93

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

DecisionChoiceWhy
Refresh interval5 min for data, 30 min for expensive or rate-limited callsBalances freshness vs cost
Loading spinnerOnly on first loadUsers see stale data briefly rather than spinner on every tab
Force parameterDefaults to falsePages call without force; mutations call with force: true
Cubit placementRoot MultiBlocProviderCubit persists across navigation, cache survives tab switches

Checklist

When adding smart caching to a cubit:

  • Add DateTime? _fetchedAt field
  • Add static const _staleAfter with appropriate duration
  • Add bool force = false parameter 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: true to 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 as BlocProvider.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's initState
  • Showing loading spinner on every navigation (users see flicker)
  • Not forcing refresh after add/delete/update mutations

Sources

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.