Sub topic 3
Your first provider: exposing a read-only value
This chapter assumes you know Dart classes, generics, and Flutter’s build method. You should also understand that a widget rebuilds when the data it displays changes.
A Riverpod provider is a declaration that creates or exposes a value. A basic Provider<T> is read-only: it calculates a value, and other parts of the app can read it. The provider itself is not a widget and does not belong to one widget’s local state.
Declare a provider
Add flutter_riverpod to pubspec.yaml, then declare a provider at the top level of a Dart file:
final greetingProvider = Provider<String>((ref) {
return 'Hello, Riverpod';
});Provider<String> means that this provider exposes a String. The callback receives a Ref object. You can use ref to read other providers or register lifecycle behavior. This example does not need it, so the parameter is unused.
Providers are usually declared outside widget classes. This gives them a stable identity and lets multiple widgets share the same value without passing it through constructors.
Read a provider from a widget
A provider needs a ProviderScope somewhere above the widgets that use it. ProviderScope creates the container that stores provider state and manages provider dependencies.
A ConsumerWidget is a widget that receives a WidgetRef. Use that reference to interact with providers:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final greetingProvider = Provider<String>((ref) {
return 'Hello, Riverpod';
});
void main() {
runApp(
const ProviderScope(
child: MyApp(),
),
);
}
class MyApp extends ConsumerWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final greeting = ref.watch(greetingProvider);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Greeting')),
body: Center(child: Text(greeting)),
),
);
}
}ref.watch(greetingProvider) reads the current value and subscribes the widget to changes. If the provider later produces a different value, Riverpod rebuilds this widget.
The value is computed when it is needed, then cached by the provider container. If several widgets watch the same provider, they can share that cached result.
Why use a provider for a constant value?
A constant could be written directly in the widget. A provider becomes useful when the value has dependencies or needs to be replaced in a test. For example, a provider could expose an API client, configuration object, or repository.
Keep provider declarations focused. A provider should expose one useful value or dependency, rather than becoming a general-purpose storage object.
Exercise
What is the difference between declaring a value directly inside a widget and reading it with ref.watch from a Provider?
Show answer
A value declared inside the widget belongs to that widget's build method. A Provider gives the value a shared, independently managed identity, and ref.watch also lets the widget rebuild when the provider's value changes.