Table of Contents
What is GetX?
GetX is not only a state management library but it is an ultra light, fast, reliable microframework for flutter which provides route management and dependency injection as well.
Flutter is moving very fast and gaining a favorite place amongst developers. As a developer, we need to cope up with frequent flutter updates, its plugins and other dependencies. Sometimes, it becomes a nightmare to manage such dependencies. Getx solved this problem, it is all in one package which makes our job quite comfortable. Also, by using GetX, we can avoid the use of StatefulWidget; it reduces the RAM consumption as well.
Let’s deep dive in it
Core principles of GetX
1. Performance
Core focus of GetX is on performance and less utilization of resources. Due to no use of Streams and ChangeNotifier, Getx is the best option among the rest of state management techniques.
With most platforms, we have to release the controller once it is not in use to optimize the memory usage but GetX doesn’t keep the controller in memory by default if it is not in use. However, we can explicitly set the controller to permanent true if required.
2. Productivity
Easy to remember syntax makes the developer job easy and overall increases productivity.
3. Organization
Decoupling of view, business login, dependency injection and navigation makes it better than any other approach to organize the code. GetX manages its own dependencies so we don’t require it to inject our own controller, model or Blocs classes in the widget tree.
Features of GetX
1. State management
Implementing Reactive programming is very complex task but with GetX, it is quite simple:
- No need to create StreamController
- No need to create Stream Builder
- No need to create class for each state
- No need to create initial value
- Then what needs to be done?
Make the variable observable as shown below:
var name = ‘Foo’.obs;
Obx(() => Text(“${controller.name}”));
That’s it, you are done. All widgets will be updated wherever the variable has been used.
2. Route management
With simple and less code, you can easily navigate to any screen through GetX. Surprise??
Just needs to follow some basic steps:
Add “Get” before your app name
GetMyApp( // Before: MaterialApp
(
home: MyHome(),
)
To navigate to new screen just
Get.to(NextScreen());
There are many other possibilities like goback, gotospecificpage and many more
3. Dependency management
GetX provides a very simple and easy way to add dependencies without need of context. Suppose we navigate to a specific page by numerous routes, we can get the value in our controller with just the find() method.
4. Localization
Providing support for multiple languages is very easy and straightforward with GetX. By following below simple steps, you can implement translation in your flutter app.
Create custom class and extend with translation and add text for required language support
class Messages extends Translations {
@override
Map > get keys => {
‘
en_US’: {
‘
surname’: ‘Surname’,
},
‘de_DE’: {
‘
surname’: ‘Familien – oder Nachname’,
}
};
}
Use just append .tr to the key which you want to translate
Text(‘surname’.tr);
You can even pass the argument to the text.
5. Storage
GetX provides extra light weight key-pair value in memory storage capabilities. Hence, we can get persistent data throughout the application if needed.
6. Theme
Theme creation with GetX becomes easier compared to anything else. Just need to create a custom theme and pass it in the Get.changeTheme() function without any boilerplate and done.
Get.changeTheme(ThemeData.light());
7. Responsive view
Providing support for multiple devices is sometimes very difficult. GetX provides the feature to quickly develop applications to support multiple dimensions e.g. desktop, tablet, mobile etc.
Conclusion
Thanks for taking the time to read the blog. Feel free to reach us if you need any further information about this topic or any other topic. I would be more than happy to assist you on the same. Let’s GetX in your next flutter app.