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.

Libraries

easy_get_it