Riverpod boilerplate

Flutter tips Published on

Are you tired of writing ref.read(myProvider) or ref.watch(myProvider) all the time in your Riverpod code? There's a neat tip to make your code cleaner and shorter.

The Problem with Repetitive Code

When you use Riverpod, you often need to get a provider's value using ref.read() or ref.watch(). Doing this repeatedly for the same provider can make your code look busy, like ref.read(userSessionRepositoryProvider).someMethod(). This adds extra text that might feel like unnecessary "boilerplate."

The Simple Solution: Use Extensions on Ref

You can create special additions, called extensions, directly on the Ref object provided by Riverpod. This lets you add your own custom ways to get providers without writing read() or watch() every time.

Here's how it might look:

extension UserSessionRepositoryProvider on Ref {
  UserSessionRepository get userSessionRepository =>
      read(userSessionRepositoryProvider);

  UserSessionRepository get userSessionRepository$ =>
      watch(userSessionRepositoryProvider);
}

How These Extensions Work

In this example, we create an extension specifically for accessing a UserSessionRepository. We add getters like userSessionRepository. This getter simply calls read(userSessionRepositoryProvider) for you behind the scenes. Now, instead of writing ref.read(userSessionRepositoryProvider), you can just write the much shorter ref.userSessionRepository.

Accessing Streaming Data

The example also shows userSessionRepository$. The $ at the end is just a common way developers show that this property might give you a stream or something that changes over time, accessed here using watch(). You could name it differently or just use watch within a regular getter if that works better for your project style.

Write Cleaner Code

Using these extensions helps you write code that's easier to read and shorter. As shown in the image's other code block, you can now just use ref.userSessionRepository directly where you need the repository instance, cutting down on boilerplate.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is this Riverpod tip about?

It's about using extensions on the Riverpod Ref object to reduce the amount of repetitive code like ref.read() or ref.watch().

Why use extensions on Ref?

Using extensions makes accessing your providers shorter and cleaner, reducing boilerplate code.

How do I create a Ref extension?

You write `extension YourExtensionName on Ref { ... }` and add getters or methods inside to access your providers.

What does the $ mean in `userSessionRepository$`?

The $ is a common naming convention some developers use to indicate that the property might return a stream or something that can be watched for changes, typically using ref.watch().

Read more
You may also be interested in
Flutter tips: how to create a responsive layout  blog card image
Flutter tips: how to create a responsive layout
Published on 2025-05-03
How to open the system app settings page  blog card image
How to open the system app settings page
Published on 2025-05-02
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved