Sub topic 2
Passing Dependencies Through Constructors
Assume you know Dart classes, abstract classes, implements, Future, async/await, Flutter widgets, and ChangeNotifier. This chapter focuses on constructor injection: passing an object's required collaborators into its constructor instead of creating them inside the object.
What constructor injection solves
A dependency is an object that another object needs to do its work. For example, a view model may depend on a repository to load user data.
Hard-coding that repository creates tight coupling:
class ProfileViewModel extends ChangeNotifier {
final ApiUserRepository _repository = ApiUserRepository();
Future<void> load(int id) async {
// Uses the concrete API repository directly.
}
}ProfileViewModel now decides which repository to use. This causes two problems:
- Changing the data source requires changing the view model.
- A unit test must use the real API repository unless the code is changed.
Constructor injection moves that decision outside the class. The class receives the object it needs:
The view model owns its behavior. The application setup owns object creation.
Depend on an abstraction
An abstraction describes the operations a dependency provides without fixing its implementation. In Dart, an abstract class can define that contract.
import 'package:flutter/foundation.dart';
abstract class UserRepository {
Future<String> fetchName(int id);
}
class ApiUserRepository implements UserRepository {
@override
Future<String> fetchName(int id) async {
// A real implementation could call an HTTP API here.
return 'User $id';
}
}
class ProfileViewModel extends ChangeNotifier {
ProfileViewModel(this._repository);
final UserRepository _repository;
String? name;
Future<void> load(int id) async {
name = await _repository.fetchName(id);
notifyListeners();
}
}
void main() async {
final repository = ApiUserRepository();
final viewModel = ProfileViewModel(repository);
await viewModel.load(42);
print(viewModel.name); // User 42
}ProfileViewModel depends on UserRepository, not ApiUserRepository. Any class that implements the contract can be supplied later.
This design also makes the dependency visible. A reader can inspect the constructor and immediately see what the view model requires.
The composition root
The composition root is the part of an application where concrete objects are assembled. In a Flutter app, it is often near main() or the top-level app widget.
void main() {
final repository = ApiUserRepository();
final viewModel = ProfileViewModel(repository);
runApp(MyApp(viewModel: viewModel));
}The rest of the application receives the already-created objects. It does not need to know how to construct the repository.
Common errors
A frequent mistake is injecting a concrete type when the class only needs an abstraction:
class ProfileViewModel {
ProfileViewModel(ApiUserRepository repository);
}This prevents tests and other implementations from being substituted easily. Use UserRepository instead.
Another mistake is creating a second dependency inside the class. That bypasses injection and makes the object partly controlled by hidden state.
Constructor injection does not require a package. Provider, get_it, and Riverpod can help deliver already-constructed objects through a larger app, but the class itself can remain ordinary Dart code.
Exercise
Why should ProfileViewModel accept UserRepository instead of creating ApiUserRepository internally?
Show answer
It keeps the view model independent of a concrete data source, so the app can change implementations and tests can provide a fake repository.