Functionalities

An Overview of Flutter State Management with GetX

logo

DhiWise

June 25, 2022
image
Author
logo

DhiWise

{
June 25, 2022
}

Whether you are an Android or iOS developer you will always come to the point in app development where you need to share the data and states between your screens and across the whole application. This is where you need an excellent library that helps you to build the Flutter app efficiently.

For Flutter state management you can find many libraries such as Provider, BloC, Redux, RiverPod, Mobx, and so on. But, there is a more simplified state management solution available for Flutter apps. 

In this article, we are going to discuss the most popular state management library known as GetX which simplifies state management, navigation, validation, and more. 

So let's start with the GetX introduction.

Introduction to GetX- A Flutter state management library

GetX combines state management, navigation management, and intelligent dependency management. It is a faster, stable, and lightweight library that helps to improve developers' productivity. GetX GitHub has 6.9k stars and more than 144 contributors.

GetX sticks to three basic principles i.e Performance, Productivity, and Organization

  1. Performance: It is focused on the minimum consumption of resources and it does not use Streams and Change Notifier.
  2. Productivity: Simple, intuitive, and efficient tools along with straightforward syntax save hours of development time.
  3. Organization: It decouples the business logic from view, presentation logic, dependency injection, and navigation. As a result, you do not need context to navigate between routes, and also you do not need to be dependent on the widget tree.

To use this package as a library run the command given below.

flutter pub add get

This will add the following line to your package’s pubspec.yaml and run an implicit flutter pub get:

dependencies:
  get: ^4.6.5

Now in your Dart code .i.e main.dart file, you can use:

import 'package:get/get.dart';

Flutter GetX features

Performance

The library provides high-performance and minimum consumption of internal resources. 

Decoupling

Saprates UI from the view, business logic, and dependencies using GetX Binding.

State Management

It has highly intuitive state management features. It also reduces the amount of boilerplate code.

Route Management

Provides navigation APIs, manages the routes of the application, and even passes parameters between the screens in a simple way with just a few lines of code.

Dependency Management

GetX offers an efficient way to manage dependencies such as controllers. It removes the controller if not being used automatically from the memory.

Internationalization

It helps you to easily add support for multiple language translation with the key-value maps using translation with singular, plurals, and parameters. Switching between the languages can automatically update your Text widgets with GetX.

Storage

The GetX storage is built completely using the Dart programming language and it can be easily integrated with the GetX Flutter framework. It provides faster synchronization with the key-value memory backup of data.

Themes

It simplifies switching between the light and dark themes. 

Responsive view

Consider building an application for the different screen sizes. With GetX you just need to extend with GetView so you can quickly develop your UI. This UI will be responsive for desktops, tablets, mobiles, and watches.

The three pillars of  GetX library

The three pillars of GetX are State Management, Route Management, and Dependency Management.

1. State Management

GetX has three different state managers:

Get Builder- Simple state manager

The Get Builder is extremely light and easy, it does not use ChangeNotifier and best meets the needs of developers who are new to Flutter. However, with the update() method your widgets can listen to the changes in your state.  

After doing some changes to your state in the controller, you need to call the update method to notify widgets that keep listening to the state.

GetX/Obx- Reactive state manager

In the Reactive, the data depends on the stream. When the data is updated all its listeners will get updated immediately. That means with Reactive state management you don’t need to use the update() method anymore.

Overall, with GetX/Obx you don't need to create,

  • StreamControllers.
  • StreamBuilder for each variable.
  • A class for each state.
  • A get for an initial value.
  • And you will not need to use code generators.
MixinBuilder- Mixing the two-state managers.

It is used in very rare cases, as few people opened a feature request to use the one type of reactive variable, and other mechanics. So they wanted to insert Obx into a GetBuilder. To fulfill this request MixinBuilder was created. It allows both i.e Reactive changes and Mechanical updates.

Image
2. Route Management

If you want to use routes/snack bars/dialogs/ bottom sheets without a context or want to use high-level Get APIs then just add “Get” before your Material App.

GetMaterialApp( // Before: MaterialApp(
  home: MyHome(),
)

3. Dependency Management

Get has a simple yet powerful dependency manager. You can retrieve the same class as your Bloc or controller using a single line of code, no Provider context, no inherited Widget.

Controller controller = Get.put(Controller());

Summing Up:

In the article, we have seen how GetX simplifies Flutter state management. The primary goal of this article is to provide you with a quick overview of the GetX library, and its benefits. GetX helps to improve your Flutter app performance while reducing the number of lines of code.

Well, if you are looking for something that helps you effectively build a Flutter application, DhiWise is here to help you. 

The LowCode/ProCode platform empowers developers to speed up the web and mobile application development by offering out-of-the-box features such as Figma to Flutter, State management, and design customization.

Its Flutter app builder supports the GetX library along with the other useful libraries to improve developers' productivity and reduce the number of lines of code. 

Get familiar with the DhiWise and sign up for free to start using its ultimate Flutter app builder.