easy_get_it 0.0.6
easy_get_it: ^0.0.6 copied to clipboard
A simple and lightweight service locator for managing dependencies in Flutter and Dart applications.
EasyGetIt #
A lightweight and minimal service locator for Dart/Flutter, inspired by GetIt. It provides a simple way to register and resolve dependencies without unnecessary complexity.
β¨ Features #
- πΉ Singleton support (pre-created instance)
- πΉ Lazy Singleton (created on first request)
- πΉ Factory (new instance every time)
- πΉ No external dependencies
- πΉ Simple and intuitive API
- πΉ Utilities for checking, unregistering, and resetting dependencies
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
easy_get_it: ^1.0.0
Then run:
flutter pub get
π Quick Start #
import 'package:easy_get_it/easy_get_it.dart';
void main() {
// Register dependencies
getIt.registerSingleton<ApiService>(ApiService());
getIt.registerLazySingleton<AuthService>(() => AuthService());
getIt.registerFactory<UserController>(() => UserController());
// Resolve dependencies
final api = getIt.get<ApiService>();
final auth = getIt.get<AuthService>();
final controller1 = getIt.get<UserController>();
final controller2 = getIt.get<UserController>(); // new instance
}
π§ Registration Types #
1. Singleton #
A single shared instance is used throughout the app.
getIt.registerSingleton<MyService>(MyService());
2. Lazy Singleton #
The instance is created only when it is first requested, then cached.
getIt.registerLazySingleton<MyService>(() => MyService());
3. Factory #
A new instance is created every time it is requested.
getIt.registerFactory<MyService>(() => MyService());
π Resolving Dependencies #
final service = getIt.get<MyService>();
If the type is not registered, an exception is thrown:
Exception: Service of type MyService is not registered
π Utilities #
Check if registered #
if (getIt.isRegistered<MyService>()) {
// Do something
}
Unregister a dependency #
getIt.unregister<MyService>();
Reset all dependencies #
getIt.reset();
βοΈ Resolution Order #
When calling get<T>(), dependencies are resolved in this order:
- Singleton
- Lazy Singleton (then cached as Singleton)
- Factory
π Global Access #
EasyGetIt is designed as a global singleton:
final EasyGetIt getIt = EasyGetIt._internal();
You can access getIt anywhere in your app without additional setup.
β οΈ Limitations #
- No scoped dependencies
- No async initialization support
- Not thread-safe (generally fine for Flutter apps)
π‘ When to use #
- Small to medium Flutter applications
- Prototyping and fast development
- When a full DI framework is unnecessary
π License #
MIT
π€ Contributing #
Contributions, issues, and feature requests are welcome.
