Flutter › Dependency Injection in Flutter (GetIt & Service Locator)
What is the GetIt package, and how does it function as a service locator, letting different parts of a Flutter app conveniently access shared service instances without needing them to be manually passed down through many widget constructors?
BeginnerAnswer
GetIt is a popular, simple service locator package for Dart and Flutter that lets you register a specific service instance once in a central location, then later retrieve that exact same instance from anywhere else within your app simply by asking GetIt for it, rather than needing to explicitly and repeatedly pass that dependency down manually through many separate intermediate widget constructors that may not otherwise have any actual direct need for that specific dependency themselves, which is often referred to as avoiding what is sometimes called prop drilling.
final getIt = GetIt.instance;
getIt.registerSingleton<ApiClient>(ApiClient());
// Later, from anywhere in the app
final apiClient = getIt<ApiClient>();Real-world example A large Flutter app registers its single shared ApiClient instance with GetIt once during app startup, letting any screen throughout the entire app retrieve that exact same shared instance directly, without needing that dependency to be manually passed down through several unrelated intermediate widgets.