Sub topic 6
Keeping State Inside the Feature
Assume you can build Flutter widgets, use Dart imports and exports, and have basic experience with a state-management approach and the repository pattern.
A feature should own the state that exists to support that feature. A cart feature can own its item list, loading status, and error message. A catalog feature can own filters and selected products. Keeping this state together makes the feature easier to understand, test, and remove.
State usually has three parts:
- Data, such as the current cart items.
- Status, such as loading, loaded, or failed.
- Actions, such as loading items or adding an item.
A widget should display state and send user actions to a state controller. It should not contain repository calls or business rules.
A feature-scoped state controller
The controller below uses Flutter's ChangeNotifier. The same separation works with Riverpod, Bloc, Provider, or another state-management library.
// lib/features/cart/presentation/cart_controller.dart
import 'package:flutter/foundation.dart';
import '../domain/cart_item.dart';
import '../domain/cart_repository.dart';
class CartController extends ChangeNotifier {
CartController(this._repository);
final CartRepository _repository;
List<CartItem> _items = const [];
bool _isLoading = false;
Object? _error;
List<CartItem> get items => List.unmodifiable(_items);
bool get isLoading => _isLoading;
Object? get error => _error;
Future<void> load() async {
_isLoading = true;
_error = null;
notifyListeners();
try {
_items = await _repository.loadItems();
} catch (error) {
_error = error;
} finally {
_isLoading = false;
notifyListeners();
}
}
Future<void> addItem(CartItem item) async {
await _repository.addItem(item);
_items = await _repository.loadItems();
notifyListeners();
}
}The controller belongs to the cart feature because its state and actions have meaning only for the cart. The repository interface belongs in domain, while the concrete repository belongs in data. The controller depends on the interface, so it does not know whether data comes from memory, a REST API, or a database.
A page can listen to this controller and rebuild when notifyListeners() runs. It should not import the concrete data source directly.
Choose the smallest useful scope
Not all state needs the same lifetime.
- Widget state, such as whether a menu is open, stays in the widget with
StatefulWidget. - Feature state, such as cart contents or checkout validation, stays in the feature's controller or notifier.
- App state, such as the signed-in user, can be shared through a small app-level contract.
- Temporary navigation data should usually travel through route arguments rather than a global store.
Create a shared state object only when multiple parts of the application genuinely need the same state. Do not make every controller global “just in case.” Global state increases lifetime, coupling, and the number of places that can change a value.
Common state mistakes
A feature boundary becomes weak when:
- Widgets call HTTP clients or database APIs directly.
- A global store contains unrelated state from many features.
- One feature imports another feature's
dataorpresentationfolders. - A controller mixes navigation decisions with data loading.
- Loading and error states are omitted, so the UI displays stale or ambiguous data.
Keep feature state close to the code that owns its rules. Expose only the data or capability that another feature needs.
Exercise
Where should a cart loading flag live, and what should checkout depend on if it only needs the item count?
Show answer
The loading flag should live in the cart feature's state controller. Checkout should depend on a small shared CartSummary interface, not on the cart controller or its private files.