Riverpod With Claude Code in Flutter: A Working Setup

· 4 min read

State management is where AI-written Flutter code most often drifts: one screen uses setState, another a global singleton, a third a provider from a tutorial. Riverpod gives you one consistent pattern, and Claude Code follows a pattern well if you write it down. This page covers a working Riverpod setup, the rules to give Claude Code and how to test it. It uses Riverpod’s documentation, read on the day this post was written, and the conventions MobileKit’s Flutter standards use.

The packages

Riverpod’s documentation says it “comes as a main riverpod package that’s self-sufficient, complemented by optional packages for using code generation and hooks.” For a Flutter app you add flutter_riverpod. For code generation you add riverpod_annotation as a dependency and riverpod_generator and build_runner as development dependencies:

flutter pub add flutter_riverpod riverpod_annotation
flutter pub add dev:riverpod_generator dev:build_runner

Then run the generator whenever you add or change an annotated provider:

dart run build_runner build --delete-conflicting-outputs

Check the current version numbers on the documentation and on pub.dev rather than copying them from a post.

Wrap the app

Riverpod’s documentation shows the entry point: the app must be wrapped in a ProviderScope.

void main() {
  runApp(ProviderScope(child: MyApp()));
}

The core ideas

From Riverpod’s provider documentation:

  • A provider has a Ref: “an object used to interact with other providers.” Every provider has one.
  • There are two modifiers: autoDispose, “which will automatically clear the cache when the provider stops being used”, and family, “which enables passing arguments to your provider.”
  • With code generation, you can disable auto-dispose with @Riverpod(keepAlive: true).
  • A notifier’s public methods are called from the UI with ref.read(yourProvider.notifier).yourMethod().

The rules to put in CLAUDE.md

These are MobileKit’s conventions for Riverpod, and a good short list to give an agent:

  • Use notifiers built with code generation. One notifier per screen concern. Return an async value.
  • ref.watch in build, ref.read in callbacks, ref.listen for side effects such as navigation or a message.
  • Use keepAlive: true only for app-wide state, such as authentication, entitlements and settings. Everything else should auto-dispose.
  • Use family for data keyed by an ID. Never use a global mutable singleton.
  • Repositories wrap SDKs. Widgets never talk to Firebase or a purchases SDK directly.
  • In tests, override the repositories, not the SDKs.

Put the rules in your CLAUDE.md, and keep them short. See CLAUDE.md for Flutter.

Handle every async state

Every screen that loads data should handle all three states of an async value: data (including the empty case), loading and error with a retry. Ask Claude Code to build a reusable shape for each, a skeleton that matches the layout while loading, a human error message with a retry button, and a friendly empty state, and then require every new screen to use them. A screen that handles only the happy path is incomplete.

Test it

Riverpod makes testing easy because dependencies are injected through providers. In a test, create a container or wrap the widget with a ProviderScope and override the repository provider with a fake. You then test the notifier’s behaviour with no network and no SDK. See how to test a Flutter app with Claude Code.

Prompts that work

  • “Create a notifier for the entries feature with code generation. It exposes the list as an async value, with methods to add and delete. Use the repository provider, not Firestore directly.”
  • “Convert this screen from setState to Riverpod following the rules in CLAUDE.md. Show me the plan first.”
  • “Write a widget test that overrides the repository with a fake that returns an empty list, and asserts the empty state shows.”

Mistakes an agent will make, and how to catch them

  • Calling ref.read in build. The widget will not rebuild when the state changes. The rule above prevents it.
  • Making everything keepAlive. State then never clears. Review every use.
  • Writing logic in the widget. Move it to the notifier so it can be tested.
  • Forgetting to re-run the generator, then fixing “missing generated code” errors by hand. Never edit generated files.

Protect generated files with a hook if you want. See Claude Code hooks for Flutter.

Architecture context

Riverpod is the state layer. Where the files live is a separate decision, and the feature-first layout in Flutter app architecture puts each feature’s notifiers in an application folder between its data and its screens.

Where MobileKit fits

MobileKit’s Flutter standards skill carries these Riverpod rules, along with the rules for errors, theming, performance and accessibility, and loads when Flutter code is written or reviewed. Flutter’s official plugin adds Flutter and Dart knowledge to the same agent. See Build a Flutter app with Claude Code.

Sources read on the day this post was written: Riverpod: getting started and Riverpod: providers.

Questions

Which Riverpod packages do I install?

Riverpod's documentation describes a main riverpod package, which for Flutter is flutter_riverpod, with optional packages for code generation (riverpod_annotation, plus riverpod_generator and build_runner as development dependencies) and for hooks. Check the current versions in the documentation.

What does autoDispose do?

Riverpod's documentation says the autoDispose modifier automatically clears a provider's cache when the provider stops being used. With code generation, you disable that with keepAlive: true for state that must live for the whole app.

Do I have to use code generation?

No. It is optional, but it reduces boilerplate and gives an agent one consistent way to write a provider, which makes its output more predictable.

Try it on your next idea

One payment, every update to this major version, and a refund window if it is not for you. Refund policy.