easy_get_it 0.0.5
easy_get_it: ^0.0.5 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.
