State Management

Get has two different state managers: the simple state manager (we'll call it GetBuilder) and the reactive state manager (GetX/Obx)

Sample Diagram

Overview

GetX combines high-performance state management, intelligent dependency injection, and route management quickly and practically.

Installing

Add Get to your pubspec.yaml file:

Alternative

Using infinity_core module we comind all of everything into one

Counter App with GetX

I will demonstrate how to make a "counter" changing the state with each click

  • Step 1: Add "Get" before your MaterialApp, turning it into GetMaterialApp

Note: this does not modify the MaterialApp of the Flutter, GetMaterialApp is not a modified MaterialApp, it is just a pre-configured Widget. GetMaterialApp will create routes, inject them, inject translations, inject everything you need for route navigation

  • Step 2: Create your business logic class and place all variables, methods and controllers inside it. You can make any variable observable using a simple ".obs".

  • Step 3: Create your View, use StatelessWidget and save some RAM, with Get you may no longer need to use StatefulWidget.

This is a simple project but it already makes clear how powerful Get is. As your project grows, this difference will become more significant

Route management

If you are going to use routes/snackbars/dialogs/bottomsheets without context, GetX is excellent for you too, just see it:

Add "Get" before your MaterialApp, turning it into GetMaterialApp

Noticed that you didn't have to use context to do any of these things? That's one of the biggest advantages of using Get route management. With this, you can execute all these methods from within your controller class, without worries.

Dependency management

Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller with just 1 lines of code, no Provider context, no inheritedWidget:

Note: If you are using Get's State Manager, pay more attention to the bindings API, which will make it easier to connect your view to your controller.

Instead of instantiating your class within the class you are using, you are instantiating it within the Get instance, which will make it available throughout your App. So you can use your controller (or class Bloc) normally

Tip: Get dependency management is decoupled from other parts of the package, so if for example, your app is already using a state manager (any one, it doesn't matter), you don't need to rewrite it all, you can use this dependency injection with no problems at all

Imagine that you have navigated through numerous routes, and you need data that was left behind in your controller, you would need a state manager combined with the Provider or Get_it, correct? Not with Get.

You just need to ask Get to "find" for your controller, you don't need any additional dependencies:

And then you will be able to recover your controller data that was obtained back there:

Binding with GetPage

In every each pages(screen) should be binding with the controller

GetPage Middleware

The GetPage has now new property that takes a list of GetMiddleWare and run them in the specific order.

Note: When GetPage has a Middlewares, all the children of this page will have the same middlewares automatically.

Redirect

This function will be called when the page of the called route is being searched for

onPageCalled

This function will be called when this Page is called before anything created you can use it to change something about the page or give it new page

OnBindingsStart

This function will be called right before the Bindings are initialize. Here you can change Bindings for this page.

OnPageBuildStart

This function will be called right after the Bindings are initialize. Here you can do something after that you created the bindings and before creating the page widget.

OnPageBuilt

This function will be called right after the GetPage.page function is called and will give you the result of the function. and take the widget that will be showed.

OnPageDispose

This function will be called right after disposing all the related objects (Controllers, views, ...) of the page.

Other APIs

BaseView

Is a const Stateless Widget that has a getter controller for a registered Controller, that's all.

Reference

Pub.dev -> https://pub.dev/packages/get#about-get

Last updated