Question #198EasyState Management

Difference between Change Notifier vs Value Notifier in providers?

#provider

Answer

Overview

Both

text
ChangeNotifier
and
text
ValueNotifier
are Flutter's built-in classes for reactive state management.
text
ValueNotifier
is a specialized, simpler version of
text
ChangeNotifier
for a single value.


ChangeNotifier

A class that can hold multiple state fields and notify listeners when you explicitly call

text
notifyListeners()
.

dart
class CartNotifier extends ChangeNotifier {
  List<CartItem> _items = [];
  double _total = 0.0;
  bool _isLoading = false;

  List<CartItem> get items => _items;
  double get total => _total;
  bool get isLoading => _isLoading;

  void addItem(CartItem item) {
    _items.add(item);
    _total += item.price;
    notifyListeners(); // ← Manually trigger rebuild
  }

  void removeItem(int index) {
    _total -= _items[index].price;
    _items.removeAt(index);
    notifyListeners();
  }

  Future<void> loadItems() async {
    _isLoading = true;
    notifyListeners();
    _items = await CartRepository.fetch();
    _isLoading = false;
    notifyListeners();
  }
}
dart
// Provide it
ChangeNotifierProvider(create: (_) => CartNotifier()),

// Consume it
Consumer<CartNotifier>(
  builder: (context, cart, child) => Text('Items: ${cart.items.length}'),
)

// Or
final cart = context.watch<CartNotifier>();

ValueNotifier

A simplified

text
ChangeNotifier
that holds exactly one value and auto-notifies when that value changes.

dart
// ValueNotifier<T> — wraps a single value
final counter = ValueNotifier<int>(0);
final theme = ValueNotifier<ThemeMode>(ThemeMode.light);
final name = ValueNotifier<String>('Alice');

// Update value — automatically notifies listeners
counter.value++;         // Triggers rebuild
theme.value = ThemeMode.dark;

// Listen
counter.addListener(() {
  print('Counter changed: ${counter.value}');
});

// Dispose when done
counter.dispose();
dart
// Widget
ValueListenableBuilder<int>(
  valueListenable: counter,
  builder: (context, value, child) => Text('Count: $value'),
)

Key Differences

FeatureChangeNotifierValueNotifier
State heldMultiple fieldsSingle value
NotificationManual
text
notifyListeners()
Automatic on
text
.value =
ComplexityHigher (full class)Very simple
Extends
text
Listenable
text
ChangeNotifier
Widget
text
Consumer
,
text
context.watch
text
ValueListenableBuilder
Use caseComplex state (cart, auth)Simple single value (count, toggle)

When to Use Which

ScenarioUse
Counter, toggle, boolean flag
text
ValueNotifier
Single selected value (tab, theme)
text
ValueNotifier
Cart with items + total + loading
text
ChangeNotifier
Form with multiple fields
text
ChangeNotifier
User profile with name + email + avatar
text
ChangeNotifier

Combined Example

dart
// Use ValueNotifier for simple state inside ChangeNotifier
class ThemeController extends ChangeNotifier {
  final isDark = ValueNotifier<bool>(false);

  void toggle() {
    isDark.value = !isDark.value;
    notifyListeners();
  }
}

Rule: Use

text
ValueNotifier
for simple single-value state. Use
text
ChangeNotifier
when you have complex state with multiple related fields.