Skip to main content

Getit Dart

Getit Dart

Introduction

Getit is a powerful dependency injection (DI) library for Dart, which allows you to easily manage the dependencies in your application. It provides a simple and intuitive way to organize and decouple your code, making it more maintainable and testable.

History

Getit was initially developed by Remi Rousselet and released in December 2018. It quickly gained popularity among the Dart community due to its simplicity and flexibility. Since then, it has been actively maintained and updated by Remi and a team of contributors.

Features

  1. Singleton Dependencies: Getit allows you to register and retrieve singleton instances of your classes. This ensures that only one instance of a class is created and shared throughout the application.
// Registering a singleton instance
GetIt.instance.registerSingleton<MyService>(MyService());

// Retrieving the singleton instance
final myService = GetIt.instance<MyService>();
  1. Lazy Initialization: Getit supports lazy initialization of dependencies, meaning that the dependencies are only created when they are actually needed.
// Registering a lazy dependency
GetIt.instance.registerLazySingleton<MyService>(() => MyService());

// Retrieving the lazy dependency
final myService = GetIt.instance<MyService>();
  1. Factory Dependencies: Getit also allows you to register and retrieve factory instances of your classes. Factory instances are created every time they are requested.
// Registering a factory instance
GetIt.instance.registerFactory<MyService>(() => MyService());

// Retrieving the factory instance
final myService = GetIt.instance<MyService>();
  1. Parameter Injection: Getit supports parameter injection, allowing you to inject dependencies directly into the constructor of a class.
class MyService {
final DataService dataService;

MyService(this.dataService);
}

// Registering the dependency
GetIt.instance.registerSingleton<DataService>(DataService());

// Retrieving the class with parameter injection
final myService = GetIt.instance<MyService>();
  1. Hierarchical Injection: Getit supports hierarchical injection, allowing you to create multiple instances of the same class with different dependencies.
// Creating a new instance of Getit
final getIt = GetIt.asNewInstance();

// Registering the dependency on the new instance
getIt.registerSingleton<MyService>(MyService());

// Retrieving the dependency from the new instance
final myService = getIt<MyService>();
  1. Service Locator: Getit acts as a service locator, allowing you to retrieve dependencies from anywhere in your application.
// Retrieving a dependency
final myService = GetIt.instance<MyService>();
  1. Clearing Dependencies: Getit provides a convenient way to clear all registered dependencies.
// Clearing all dependencies
GetIt.instance.reset();

Examples

  1. Creating a Singleton Dependency:
class MyService {
void doSomething() {
print('Doing something...');
}
}

// Registering the singleton instance
GetIt.instance.registerSingleton<MyService>(MyService());

// Retrieving and using the singleton instance
final myService = GetIt.instance<MyService>();
myService.doSomething(); // Output: Doing something...
  1. Creating a Lazy Dependency:
class MyService {
void doSomething() {
print('Doing something...');
}
}

// Registering the lazy dependency
GetIt.instance.registerLazySingleton<MyService>(() => MyService());

// Retrieving and using the lazy dependency
final myService = GetIt.instance<MyService>();
myService.doSomething(); // Output: Doing something...
  1. Creating a Factory Dependency:
class MyService {
void doSomething() {
print('Doing something...');
}
}

// Registering the factory instance
GetIt.instance.registerFactory<MyService>(() => MyService());

// Retrieving and using the factory instance
final myService = GetIt.instance<MyService>();
myService.doSomething(); // Output: Doing something...
  1. Injecting Dependencies:
class MyService {
final DataService dataService;

MyService(this.dataService);

void doSomething() {
dataService.fetchData();
}
}

class DataService {
void fetchData() {
print('Fetching data...');
}
}

// Registering the dependencies
GetIt.instance.registerSingleton<DataService>(DataService());
GetIt.instance.registerSingleton<MyService>(MyService(GetIt.instance<DataService>()));

// Retrieving and using the class with parameter injection
final myService = GetIt.instance<MyService>();
myService.doSomething(); // Output: Fetching data...

These examples demonstrate some of the key features of Getit Dart. For more information and advanced usage, refer to the official Getit documentation.