Dependency injection

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
May 15, 2023

Flutter GetIt Package: A Dependency Injection Framework for Flutter

When building complex apps, managing dependencies can become a burden. One way to mitigate this problem in Flutter development is by using GetIt, a dependency injection (DI) framework and service locator. In this tutorial, we'll take a closer look at what GetIt is and how it can help us improve our Flutter app development experience.

What is GetIt?

GetIt is a simple yet powerful DI framework that helps with managing our app's dependencies. Instead of manually creating and managing objects, we can use GetIt to create and store them for us, so we can access the objects wherever we need them throughout our app.

GetIt is designed to be easy to use and configure in our Flutter projects. It provides a singleton pattern, allowing us to declare dependencies with little boilerplate and a short amount of code.

Installing GetIt

Getting started with GetIt is easy. We can install it by adding the following to our project's `pubspec.yaml` file:

dependencies:
  get_it: ^7.2.0 }

Don't forget to run `flutter pub get` after updating the `pubspec.yaml` file.

Using GetIt for Dependency Injection

Now that we have GetIt installed and ready, let's take a look at how we can use it to achieve DI in our Flutter app.

First, we need to declare our objects. These can be any class or object that we would like to use throughout our app:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // We'll declare our objects in our app's constructor
    GetIt.I.registerSingleton(Foo());
    GetIt.I.registerFactory(() => Bar());
  
    return MaterialApp(
      title: 'My App',
      home: HomeScreen(),
    );
  }
}

class Foo {
  String message = "Hello, world!";
}

class Bar {
  String message = "Goodbye, cruel world!";
}

In this example, we've declared two objects that can be used throughout our app: `Foo` and `Bar`. We've used both `registerSingleton` and `registerFactory` here: `registerSingleton` means that GetIt will only ever create one instance of `Foo` and that we can get the instance through GetIt by calling `GetIt.I()`. `registerFactory`, on the other hand, means that we can call `GetIt.I()` multiple times and GetIt will create a new instance of `Bar` each time.

Now that we've declared our objects, we can use them in our app:

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(GetIt.I().message),
            Text(GetIt.I().message),
          ],
        ),
      ),
    );
  }
}

In our `HomeScreen`, we're accessing the `message` field of both `Foo` and `Bar` by using `GetIt.I()` and `GetIt.I()`, respectively.

Conclusion

GetIt is a simple and easy-to-use DI framework and service locator that can help us manage dependencies in our Flutter projects. With GetIt, we can register objects once and access them throughout our app with ease. By providing us with a singleton pattern, GetIt can help us reduce boilerplate and focus on building the app we want.