For reference: Flutter. Dev, Bruno (Shell Open Source Component Library)
The following is reproduced for the record from part of the GetX document. Frame: Flutter GetX
GetX is a lightweight and powerful solution for Flutter: high-performance state management, intelligent dependency injection, and convenient route management.
GetX has three basic principles:
Performance: GetX focuses on performance and minimal resource consumption. GetX’s packaged APK footprint and runtime memory footprint are comparable to other state management plug-ins. Efficiency: GetX’s syntax is very simple and maintains extremely high performance, which can greatly reduce your development time. Structure: GetX completely decouples the interface, logic, dependencies, and routing, making it cleaner to use, clearer to logic, and easier to maintain code. GetX is not bloated, but lightweight.
Three big functions
State management
Flutter currently has several state managers. However, most of them involve using ChangeNotifier to update widgets, which is a poor way to perform for medium – and large-sized applications. As you can see from the official Documentation for Flutter, ChangeNotifier should use 1 or 2 listeners at most, making them virtually unusable for any medium or large application.
Get is not better or worse than any other state manager, but rather you should analyze these points and the ones below to choose whether to use Get alone or in combination with other state managers.
Get is not the enemy of other state managers because Get is a microframework, not just a state manager, and can be used either alone or in combination with other state managers.
Get has two different state managers: the simple state Manager (GetBuilder) and the responsive state Manager (GetX).
Reactive state manager
Reactive programming may seem strange to many people because it is complex, but GetX makes reactive programming very simple.
- You don’t need to create StreamControllers.
- You don’t need to create a StreamBuilder for each variable.
- You don’t need to create a class for each state.
- You don’t need to create a GET for an initial value.
Reactive programming with Get is as easy as using setState.
Let’s imagine that you have a name variable and want all the widgets that use it to refresh automatically every time you change it.
That’s your count variable.
var name = 'Jonatas Borges';
Copy the code
To make it observable, you just add “.obs” to the end.
var name = 'Jonatas Borges'.obs;
Copy the code
In the UI, when you want to display the value and update the page when the value changes, you just do that.
Obx(() => Text("${controller.name}"));
Copy the code
That’s all. It’s as simple as that.
More details on state management
See here for a more in-depth explanation of state management. You’ll see more examples there, and the difference between a simple state manager and a reactive state manager.
You’ll get a good idea of GetX’s capabilities.
Routing management
If you want to avoid the context (context) using routing/snackbars/dialogs/bottomsheets, GetX also is very good for you, come on display:
Add “Get” to the front of your MaterialApp to make it GetMaterialApp.
GetMaterialApp( // Before: MaterialApp(
home: MyHome(),
)
Copy the code
Navigate to a new page
Get.to(NextScreen());
Copy the code
Navigate to a new page with an alias. See more details on named routing here
Get.toNamed('/details');
Copy the code
To close Snackbars, dialogs, Bottomsheets or anything else you would normally close with navigator.pop (context).
Get.back();
Copy the code
Go to the next page with no option to return to the previous page (for splash screen, login page, etc.).
Get.off(NextScreen());
Copy the code
Go to the next page and cancel all previous routes (useful in shopping carts, polls, and tests).
Get.offAll(NextScreen());
Copy the code
Notice that you don’t need to use context to do these things? This is one of the biggest advantages of using Get for route management. With it, you can execute all these methods in your controller class without worrying about where the context is.
More details on route management
For alias routing, and low-level control of routing, see here.
Dependency management
Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller in just one line of code, no Provider context, no inheritedWidgets.
Controller controller = Get.put(Controller()); // Instead of Controller Controller = Controller();Copy the code
- Note: If you are using the state manager for Get, pay attention to the binding API, which will make it easier for your interface to connect to your controller.
You’re instantiating it in a Get instance, not your class in the class you’re using, which will make it available throughout the App. So you can use your controller (or Bloc like) normally.
Tip: Get dependency management is decouple from the rest of the package, so if your application already uses a state manager (any one, doesn’t matter), you don’t need to rewrite it all, you can use dependency injection instead.
controller.fetchApi();
Copy the code
Imagine that you’ve gone through countless routes, and now you need to retrieve data that was left in the controller, so you need a state manager to use with Provider or Get_it to retrieve it, right? This is not the case with Get, which automatically finds the data you want for your controller without you even needing any additional dependencies.
Controller controller = Get.find(); // Yes, it looks like magic, Get will find your controller and provide it to you. You can instantiate a million controllers, Get will always give you the right controller.Copy the code
Then you can restore the controller data that you acquired later.
Text(controller.textFromApi);
Copy the code