Search⌘ K
AI Features

The Get.put Method

Explore how to implement the Get.put method in Flutter using GetX for dependency injection. Learn to initialize dependencies, handle multiple shared instance groups using tags, and preserve dependencies across widget lifecycles. This lesson helps you manage state independently and maintain dependencies throughout the app efficiently.

Introduction and implementation

Get.put is the method used to initialize a dependency for the first time. This method makes a dependency available throughout the widget tree, which can then be accessed via Get.find.

Get.put internally calls Get.find, which injects the dependency immediately into the class. This means that we do not have to call Get.find ourselves to use that dependency in the class.

Dart
class HomePage extends StatelessWidget {
final Controller controller = Get.put(Controller()); // Controller dependency injected.
@override
Widget build(BuildContext context) {
return Text(controller.name); // Controller can be used directly.
}
}

In the code snippet above, we inject the Controller dependency using Get.put and directly access it via the controller variable without using Get.find.

This is the simplest use case that Get.put covers. Let’s explore how we can configure it for some advanced use cases.

Multiple shared instances

...