Master The Art of Dependency Injection
What is Dependency Injection? Why Dependency Injection? How to Inject Dependencies? How to achieve Dependency Injection In Flutter?

Search for a command to run...
What is Dependency Injection? Why Dependency Injection? How to Inject Dependencies? How to achieve Dependency Injection In Flutter?

Thank you Fred π
Thank you Ginaa π Glad you liked it!!
Very huge thanks for you. Easy and very good explain and clarify the reasons why we use each way to implement the DI.
Thank you Khaled, for the feedback Really appreciate it π
Glad you liked it π Keep learning π―
Hi Dhruv, nice article. It would be nice if you could use code snippets instead of images.
For the rest, awesome explanation, I really enjoyed it.
Thanks for the feedback Michele Volpato. Sure I'll add snippets instead of images from now on.
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders β Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts



When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy don't want you to have. You might even be looking for something we don't even have or which has expired. What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.
DI is a design pattern in which a class requests dependencies from external sources rather than creating them. It means the dependent objects are injected or supplied to your class. The class doesn't have to do any initialization by itself. It will get what it wants by the injector.
// Car.dart
import 'package:blog/Engine.dart';
class Car {
Engine _engine = Engine();
void start() {
_engine.start();
}
}
start method. Which simply prints a message.// Engine.dart
class Engine {
void start() {
print("Engine Started");
}
}
// main.dart
import 'package:blog/Car.dart';
void main() {
Car car = Car();
car.start();
}
Method Injection
// Car.dart
import 'package:blog/Engine.dart';
Passing Engine object as a parameter
// Car.dart
class Car {
Engine? _engine;
Car(Engine engine) {
this._engine = engine;
}
void start() {
_engine!.start();
}
}
// main.dart
import 'package:blog/Car.dart';
import 'package:blog/Engine.dart';
constructor.// main.dart
void main() {
Engine normalEngine = Engine();
Car car = Car(normalEngine);
car.start();
}
constructor.class ElectricEngine implements Engine {
@override
void start() {
print("Electric Engine Started");
}
}
// main.dart
void main() {
// Engine normalEngine = Engine();
Engine eletricEngine = ElectricEngine();
Car car = Car(eletricEngine);
car.start();
}


InheritedWidget as Dependency InjectionInheritedWidget in the widget tree.


InheritedDI.of(context)!.getCar
context to access the object.context.dependencies inside the classes like DB, Client, etc? There we won't have context, because they're just simple regular classes.get_it as Dependency Injectionget_it package. get_it is a Service Locator.
The purpose of the Service Locator pattern is to return the service instances on demand.If you are not familiar with the concept of Service Locators, it's a way to decouple the interface (abstract base class) from a concrete implementation, and at the same time allows to access the concrete implementation from everywhere in your App over the interface.
get_it as Dependency Injection.get_it package inside pubspec.yamlget_it: ^7.2.0
serviceLocator.dart file. In this file first, create instance of GetIt.
setup() method before the app initialization i.e inside main() method

get_it makes dependency injection very easy for us.context or not.get_it provides many different ways to register dependencies. Two of the common ways are:registerFactory will give a new instance every time get() is called.void registerFactory<T>(FactoryFunc<T> func) // returns an NEW instance of an implementation of T
registerSingleton will create only one instance of the Car at the initialization. This will not generate new instances every time like registerFactory.void registerSingleton<T>(T instance) // returns a singleton instance of an implementation of T
