What is a controller?

As the name suggests, a controller controls the app’s state. It is the class where all the business logic goes. We place variables and methods in controllers and use them to manage the app’s state. A controller allows us to manage the state outside the widgetA widget is a fundamental element in Flutter that represents a visual component in the user interface, such as a button, text field, or layout container. class, which lets us organize the code more efficiently.

As a controller’s job is storing variables and methods, we can use any class. However, GetX provides a class named GetxController for the job, and there are multiple reasons to use it over any other class. So, let’s understand what GetxController is, its features, and the reasons to use it.

Understanding GetxController

The GetxController class provides a change notification and life cycle system similar to StatefulWidgetA StatefulWidget is a Flutter widget class that manages its own mutable state, allowing it to rebuild and update based on changes in its state.. Change notification allows us to notify widgets that their state has changed and that they need to rebuild. The life cycle system allows us to execute functions during a widget’s various life cycle events, such as before it’s rendered and after its disposal.

These two features allow us to manage the state effectively and remove reliance on StatefulWidget for life cycle events. For example, we can add listeners, close streams, and dispose of controllers without using a StatefulWidget.

Syntax

Use GetxController by extending it like this:

Get hands-on with 1400+ tech skills courses.