Sub topic 1
What Dependency Injection Solves
A dependency is an object that another object needs to do its work. For example, a profile view model may need a user repository to load a user's name. A repository is an object that provides access to data, such as an API or database.
Dependency injection (DI) is the practice of giving an object the dependencies it needs from outside. The object does not create those dependencies itself.
Prerequisites
This chapter assumes that you understand Dart classes, abstract classes, implements, Flutter widgets, BuildContext, Future, and async/await. You should also know the basic purpose of a state-management class such as ChangeNotifier.
The problem with creating dependencies internally
Consider a view model that creates its own repository:
class ProfileViewModel {
Future<String> loadName(int id) async {
final repository = ApiUserRepository();
return repository.fetchName(id);
}
}
class ApiUserRepository {
Future<String> fetchName(int id) async {
// A real implementation would call an API.
return 'User $id';
}
}ProfileViewModel is now tightly coupled to ApiUserRepository. Coupling is the degree to which one piece of code depends on the details of another piece.
This design creates several problems:
- The view model cannot easily use a database repository instead.
- A test must use the real API unless the code is changed.
- Network configuration is hidden inside a class that should focus on presentation state.
- Creating the repository may happen repeatedly instead of at a controlled lifetime.
- Changes to the repository constructor can force changes in unrelated classes.
The view model has two jobs: loading data and deciding which repository to construct. Separating those jobs makes each part easier to change.
A working model
With DI, the application's startup code constructs the concrete objects and connects them. The view model receives an already-created repository.
abstract class UserRepository {
Future<String> fetchName(int id);
}
class ApiUserRepository implements UserRepository {
@override
Future<String> fetchName(int id) async {
return 'User $id';
}
}
class ProfileViewModel {
ProfileViewModel(this._repository);
final UserRepository _repository;
Future<String> loadName(int id) {
return _repository.fetchName(id);
}
}
void main() async {
final repository = ApiUserRepository();
final viewModel = ProfileViewModel(repository);
print(await viewModel.loadName(7));
}The constructor parameter is the injection point. The view model depends on the UserRepository abstraction, not on the API implementation. An abstraction describes what an object can do without specifying how it does it.
The application can now choose an implementation at startup. Later chapters show how Flutter's widget tree, Provider, get_it, or Riverpod can perform this wiring.
Why testing becomes easier
A test can supply a small fake instead of making a network request:
class FakeUserRepository implements UserRepository {
@override
Future<String> fetchName(int id) async => 'Test User';
}The same ProfileViewModel can use FakeUserRepository in a test and ApiUserRepository in the running app. The view model's behavior is tested without network access, credentials, delays, or server failures.
DI also makes failures easier to locate. Construction and configuration happen in one place, while application logic remains focused on its task.
Exercise
What is the main difference between a view model that constructs its repository and one that receives a repository in its constructor?
Show answer
The first view model is coupled to one concrete implementation. The second depends on an externally supplied abstraction, so the implementation can be changed or replaced in tests.