Ein einfacher und direkter Service-Locator, der die Trennung zwischen Schnittstelle und konkreter Implementierung ermöglicht und den Zugriff auf die konkrete Implementierung von überall im App erlaubt.
Aus öffentlichen Paketdaten erstellte Orientierung.
GetIt ist ein leichtgewichtiger, einfacher Service-Locator für Dart und Flutter, der die Abhängigkeitsinjektion ermöglicht, indem Schnittstellen von ihren konkreten Implementierungen entkoppelt werden. Es ermöglicht Entwicklern, Dienste innerhalb der gesamten App global zu registrieren und abzurufen, wobei nur minimaler Boilerplate erforderlich ist. GetIt unterstützt lazy Instantiierung, Factory-Registrierung und einfaches Testen durch Mocking. Es ist ideal zum Verwalten gemeinsamer Abhängigkeiten wie Repositories, APIs oder State-Manager über verschiedene Teile einer Anwendung hinweg.
Anwendungsfälle
Verwaltung gemeinsamer Dienste über mehrere Widgets
Testen mit mockierten Abhängigkeiten
Repository und API-Clients injizieren
Einfachere Einrichtung der Zustandsverwaltung
Trennung der Benutzeroberfläche von der Geschäftslogik
Hauptmerkmale
Einfache API mit minimalem Setup
Unterstützt lazy und eager Instantiierung
Fabrik- und Singleton-Registrierung
Einfach zu mocken für Unit-Tests
Leicht und schnell
Geeignet für
Kleine bis mittelgroße Flutter-Apps
Entwickler, die neu in Abhängigkeitsinjektion sind
Projekte, die eine schnelle DI-Integration benötigen
Teams, die Einfachheit der Komplexität vorziehen
Apps, die testbare Dienstschichten erfordern
Hinweise
Nicht geeignet für großskalige Unternehmensanwendungen mit komplexen Abhängigkeitsinjektionsanforderungen
Bei unsachgemäßer Verwaltung kann die manuelle Registrierung zu Laufzeitfehlern führen
Eingeschränktes integriertes Lebenszyklus-Management im Vergleich zu fortgeschrittenen DI-Frameworks
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
A blazing-fast service locator for Dart and Flutter that makes dependency management simple.
As your app grows, you need a way to access services, models, and business logic from anywhere without tightly coupling your code to widget trees. get_it is a simple, type-safe service locator that gives you O(1) access to your objects from anywhere in your app—no BuildContext required, no code generation, no magic.
Think of it as a smart container that holds your app's important objects. Register them once at startup, then access them from anywhere. Simple, fast, and testable.
flutter_it is a construction set — get_it works perfectly standalone or combine it with other packages like watch_it (state management), command_it (commands), or listen_it (reactive operators). Use what you need, when you need it.
Why get_it?
⚡ Blazing Fast — O(1) lookups using Dart's native Maps. No reflection, no slow searches.
🎯 Type Safe — Full compile-time type checking with generics. Errors caught before runtime.
🧪 Test Friendly — Easily swap real implementations for mocks. Reset and reconfigure between tests.
🌳 No BuildContext — Access your objects from anywhere—business logic, utilities, even pure Dart packages.
📦 Framework Agnostic — Works in Flutter, pure Dart, server-side, CLI apps. No Flutter dependencies required.
🔧 Zero Boilerplate — No code generation, no build_runner, no annotations. Just register and use.
import'package:get_it/get_it.dart';
// Create a global instance (or use GetIt.instance)final getIt = GetIt.instance;
// 1. Define your servicesclassApiClient{
Future<void> fetchData() async { /* ... */ }
}
classUserRepository{
finalApiClient apiClient;
UserRepository(this.apiClient);
}
// 2. Register them at app startupvoidconfigureDependencies() {
getIt.registerSingleton<ApiClient>(ApiClient());
getIt.registerLazySingleton<UserRepository>(
() => UserRepository(getIt<ApiClient>())
);
}
// 3. Access from anywhere in your appclassMyHomePageextendsStatelessWidget{
@overrideWidgetbuild(BuildContext context) {
returnElevatedButton(
onPressed: () {
// No BuildContext passing needed!
getIt<UserRepository>().apiClient.fetchData();
},
child: Text('Fetch Data'),
);
}
}
That's it! Three simple steps: define, register, access.
Key Features
Registration Types
Choose the lifetime that fits your needs:
Singleton — Create once, share everywhere. Perfect for services that maintain state.
Read more →
LazySingleton — Create on first access. Delays initialization until needed.
Read more →
Factory — New instance every time. Great for stateless services or objects with short lifetimes.
Read more →
Avoid untyped tear-offs as factories. Registering getIt.call (or an untyped getIt.get tear-off) as registerLazySingleton<Iface>(...) re-enters get<Iface>() and used to overflow the stack. For lazy singletons (sync and async) and cached factories GetIt now detects this circular self-resolution and throws a descriptive StateError. Plain registerFactory factories are not guarded, since a factory that recursively resolves its own type (e.g. to build a tree) is a legitimate pattern. Prefer () => getIt<Impl>() or a named function that keeps the concrete type argument.
Advanced Features
Scopes — Create hierarchical registration scopes for different app states (login/logout, sessions, feature flags).
Read more →
Async Objects — Register objects that need async initialization with dependency ordering.
Read more →
Startup Orchestration — Easily orchestrate initialization of asynchronous objects during startup.
Read more →
Named Instances — Register multiple implementations of the same type with different names.
Read more →
Multiple Registrations — Register multiple implementations and retrieve them all as a collection.
Read more →
Unregistering — Remove registrations with unregister(), optionally disposing the instance. By default unregister() throws if nothing matching is registered (usually a sign that you dispose once too often); set getIt.skipUnregisterIfNotRegistered = true to silently ignore a missing registration instead.
Read more →
Testing Support
get_it makes testing a breeze:
Easy Mocking — Replace real implementations with mocks using allowReassignment or reset()
Test Isolation — reset() clears all registrations between tests
Constructor Injection — Use optional constructor parameters to inject mocks in tests
💡 Tip: Override toString() in your registered classes to see meaningful details in the DevTools extension. By default, Dart's toString() only shows the type name and instance ID.
This package ships an Agent Skill for AI coding assistants
(Claude Code, Cursor, GitHub Copilot, Codex, Gemini CLI and others) in skills/get-it-expert/.
It teaches them the critical rules, common patterns and anti-patterns of get_it.
Install it into your project with the official Dart skills tool:
dart run skills@ get
For the ecosystem-wide skills (architecture guidance, feed/data-source patterns, overview)
and the skills of the other flutter_it packages run: