...

/

The GetView and GetWidget Classes

The GetView and GetWidget Classes

Learn to use GetView and GetWidget to readily access the controller.

Simplifying dependency access

GetX provides an excellent API for segregating dependency injection into multiple classes and hiding it from the presentation layer. However, we still need to use Get.find to access the dependency in the widget. GetX takes it a step further by introducing the GetView and GetWidget widgets.

GetView

GetView is a stateless widget that internally declares a controller for us. We can readily use the controller instance without calling Get.find. Just provide the type of controller we wish to access, like this:

Press + to interact
// Provide the controller type.
class HomePage extends GetView<Controller> {
@override
Widget build(BuildContext context) {
// Access controller directly.
return Text(controller.name);
}
}

GetView provides an instance of Controller, named controller. This simple widget can replace any widget that depends on a single controller.

Hands-on example

Here we have a counter app, just like Flutter's default. We create a controller to manage the state of the counter that can be incremented by pressing the FAB. Notice how we don’t ...