Skip to main content

easy_get_it 0.0.5 copy "easy_get_it: ^0.0.5" to clipboard
easy_get_it: ^0.0.5 copied to clipboard

A simple and lightweight service locator for managing dependencies in Flutter and Dart applications.

EasyGetIt #

Pub License: MIT Points Donate

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.

repository


✨ 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:

  1. Singleton
  2. Lazy Singleton (then cached as Singleton)
  3. 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.

0
likes
155
points
68
downloads
screenshot

Documentation

API reference

Publisher

verified publisherrony.fun

Weekly Downloads

A simple and lightweight service locator for managing dependencies in Flutter and Dart applications.

Repository (GitHub)
View/report issues

Topics

#dependency-injection #service-locator #inversion-of-control #di-container #di

License

MIT (license)

Dependencies

flutter

More

Packages that depend on easy_get_it