ValueNotifier holds one value and notifies listeners when its setter receives a value considered different. It is useful for focused state without defining a larger model class.
Give the notifier an owner
The screen creates the notifier and disposes it. A ValueListenableBuilder listens and rebuilds the relevant region. Unrelated widgets can remain outside that builder.
Worked example
This is a complete example. Open it with Run example, then use Run inside DartPad.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ReadingProgress()));
class ReadingProgress extends StatefulWidget {
const ReadingProgress({super.key});
@override
State<ReadingProgress> createState() => _ReadingProgressState();
}
class _ReadingProgressState extends State<ReadingProgress> {
final completed = ValueNotifier<int>(0);
@override
void dispose() { completed.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Reading progress')),
body: Center(child: ValueListenableBuilder<int>(valueListenable: completed,
builder: (context, value, child) => Text('$value lessons completed'))),
floatingActionButton: FloatingActionButton(
onPressed: () => completed.value++, child: const Icon(Icons.check)),
);
}Read the result
Changing the integer assigns a new value. For a list, mutating the existing object in place may not notify as intended; use an immutable replacement or a model with an explicit mutation API.
Distinguish assignment from mutation inside an object
Incrementing an integer assigns a new value through the notifier’s setter, which can trigger listeners. If the value were a List, adding an item to that same list object would not necessarily pass through the setter or create an unequal value.
Use an immutable replacement for simple collection state, or expose operations on a model that deliberately sends the right notification. The important point is that the observable change must cross the notification boundary.
A ValueListenableBuilder can keep a focused region reactive while an unrelated heading remains outside it. Compare the broader model responsibilities in ChangeNotifier before turning one notifier into a container for every part of an application.
Practice and verification
Store a selected filter instead of a count. Add a separate preview that observes the same notifier. Confirm the notifier is created once, not every time build runs.