← → or space · progress saves for Continue on the roadmap
Goal
Recognize hidden globals and static lookups that undo the benefits of injection.
Pattern
class Locator {
static final Locator I = Locator._();
Locator._();
late ApiClient api;
static T get<T>() {
if (T == ApiClient) return I.api as T;
throw StateError('unknown $T');
}
}void sendPing() {
final client = Locator.get<ApiClient>();
client.post('/ping', body: null);
}Problems
- Call sites hide dependencies; you cannot see
ApiClientfrom the signature. - Tests must mutate global registry order; parallel tests race.
- Refactors miss call sites because there is no constructor to update.
Better direction
- Pass
ApiClientintosendPingor into a class that ownssendPing. - Use a container only at the edge (
main) and inject concrete dependencies inward.
Practice tasks
- Find one
getIt<...>()(or similar) inside a domain class and move the type to a constructor parameter. - List three test failures you have seen from “forgot to register mock in locator.”
- Compare pure constructor injection vs container resolves graph in two sentences for your team wiki.