ValueNotifier: Notifies on Replacement, Not Mutation
ValueNotifier is a simple state holder that tells widgets to rebuild when its single value is replaced. It's great for immutable data like a boolean toggle or counter.
Why it exists
Flutter needs a simple, built-in way to broadcast changes for a single piece of state without forcing developers into a complex state management library. ValueNotifier is the lightweight tool for this common problem: managing one value and telling others when it changes.
The mental model
A ValueNotifier is a box with a loudspeaker. The box holds a single value. The loudspeaker only goes off when you take the old value out and put a completely new one in. If you just reach in and tweak the value that's already there (like adding an item to a list), the loudspeaker stays silent because the box still contains the same list object.
How it works
ValueNotifier wraps a single value. When you assign a new object to its .value property, it compares the new value with the old one using the standard == equality operator. If they are not equal, it automatically calls notifyListeners(). Any widget or object that has registered a listener (e.g., via a ValueListenableBuilder) will then be alerted to the change, typically triggering a UI rebuild.
When to use it
Use ValueNotifier for simple, local state that doesn't warrant a heavy solution. It is perfect for immutable data types like integers, booleans, strings, or your own custom immutable classes. A classic use case is managing the state of a single counter (ValueNotifier<int>) or a theme toggle (ValueNotifier<bool>).
When not to use it
Avoid ValueNotifier with mutable objects like List or Map if you plan to modify them in-place. It will not notify listeners on mutations like myListNotifier.value.add(5) because the list object reference itself has not changed. For these scenarios, either create a new copy of the list on each change or extend ChangeNotifier directly and call notifyListeners() manually after mutations.
One canonical example
A ValueNotifier<int> for a simple counter works perfectly. When you increment, you assign counter.value = counter.value + 1. This replaces the old integer object with a new one, which correctly triggers a notification. In contrast, if you have a ValueNotifier<List<int>> and call myListNotifier.value.add(10), no notification will be sent because the list object itself is the same, even though its contents changed.
Interview question
Which operation will NOT trigger a notification from a ValueNotifier?
- a.Setting notifier.value to a different boolean
- b.Modifying an element within a Map held by notifier.valueCorrect
- c.Assigning a new integer to notifier.value
- d.Replacing a List with an entirely new List object
Why? this is the answer
The correct answer is C because modifying an element within a Map is a mutation of the existing object, not a replacement of the Map itself. ValueNotifier only notifies when its value property is assigned a new object, not when the contents of the existing object are changed. Options A, B, and D all involve assigning a new object to the value property, which triggers a notification.
Just read this? Test yourself on what you have been reading.
Read the original → api.flutter.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles