The Get.put Method
Learn to initialize dependencies using Get.put and configure its options.
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.
Press + to interact
class HomePage extends StatelessWidget {final Controller controller = Get.put(Controller()); // Controller dependency injected.@overrideWidget 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.