[GetX] Dependency Management

2022-01-24 hit count image

Let's see how to manage Denpendency by GetX in Flutter.

Outline

In this blog post, I will introduce how to manage dependencies by using GetX in Flutter.

Blog series

This blog post is made in the series about how to use GetX in Flutter. If you want to see the other features of the GetX, please check out the following blog posts.

GetX installation

To check how to use GetX in Flutter, execute the command below to create a new Flutter project.

flutter create dependency_management

And then, execute the command below to install the GetX package.

flutter pub add get

Next, let’s see how to manage the dependencies in GetX.

Get.put

Normally, to manage the dependencies, we need to use the Get.put method like the below.

Get.put(CountController());

You can assign the controller to the variable by using Get.put like the below.

final controller = Get.put(CountController());
...
print(controller.count.value);
...
controller.increment();

Get.lazyPut

When you add the controller by Get.put, the controller is created on the memory when the code is executed. However, if you use Get.lazyPut, the controller is created on the memory when the controller is used.

Get.lazyPut<CountController>(() => CountController());

Get.putAsync

If the controller returns Future, you can use Get.putAsync to create the controller asynchronously like the below.

Get.putAsync<CountController>(() async => await CountController());

Get.find

If you added the controller by Get.put, Get.lazyPut, or Get.putAsync, you can find and get the controller by using Get.find like the below.

final controller = Get.find<CountController>();
...
print(controller.count.value);
...
controller.increment();

Dependency injection when moving screen

You can use binding to create the controller and inject it when moving screen by Get.to like the below.

Get.to(Second(), binding: BindingsBuilder(() {
  Get.put(CountController());
}));

Or, you can use it with Get.toNamed like the below.

Get.toNamed('/second', binding: BindingsBuilder(() {
  Get.put(CountController());
}));

Dependency injection in route

You can use binding to inject the dependency in GetPage like the below.

getPages: [
  GetPage(name:"/", page: () => Home()),
  GetPage(name:"/second", page: () => Second(), binding: BindingsBuilder(() {
    Get.lazyPut<CountController>(() => CountController());
  })),
]

Sperate binding

As you see above, we can use binding directly in Get.to / Get.toNamed or GetPage, but you can seperate binding like the below.

class SecondBinding implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<CountController>(() => CountController());
  }
}

And then, you can use it in Get.to / Get.toNamed like the below.

Get.to(Second(), binding: binding: SecondBinding());

Also, you can use it in GetPage like the below.

getPages: [
  GetPage(name:"/", page: () => Home()),
  GetPage(name:"/second", page: () => Second(), binding: SecondBinding()),
]

permanent and GetxService

GetX will remove the controller automatically when the controller is no more used. However, if you need to load the data or the controller is used frequently, you can load the controller on the memory and keep it instead of deleting it. At this time, you can use permanent to keep the controller.

Get.put(CountController(), permanent: true);

Or, you can use GetxService instead of GetxController to keep the controller.

// class CountController extends GetxController {
class CountController extends GetxService {
  ...
}

Get.reset

If you need to reset the controller that is already on the memory, you can use Get.reset.

Get.reset();

It is usually used for the test code like the below.

setUp(() {
  Get.reset();
});

Get.remove

It’s very rare, but if you need to remove the controller that is already registerd on the memory, you can use Get.remove to remove the controller forcely.

Get.delete<CountController>();

GetX will remove the controller automatically when the controller is no more used, so this is not used normarlly.

Completed

Done! we’ve seen how to use GetX to manage the dependencies in Flutter. Also, we’ve seen how to keep the controller and reset the controller.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts