Chapter Summary
Summarize the key concepts and techniques learned in this chapter.
We'll cover the following
Summarizing local storage with GetX
In this chapter on Local Storage with GetX, we explored the GetStorage library for efficient local storage, covering its initialization, data manipulation, and change listening capabilities. Additionally, we delved into the GetxService
class, which ensures that essential services, such as database and authentication, remain alive throughout the app’s life cycle. This summary lesson will review the key concepts from this chapter’s lessons. So, let’s go through them one by one.
Storage implementation
Introduction to GetStorage:
GetStorage is GetX’s local storage solution. It is fast, lightweight, and synchronous, supporting every platform, including the web.
It is a great alternative to SharedPreferences but lacks dedicated database features like queries, making replacing a local database like Hive unsuitable.
Initialization:
Initialize the storage bucket in the
main
method to ensure it is ready before the app launches.Create an instance of
GetStorage
to access the storage bucket.Initialize and access multiple storage buckets by providing unique names.
Write data:
Insert key-value pairs into the storage using the
write
method.Conditionally write data if the value for the key does not already exist.
Add multiple entries and save them together for batch writing.
Read data:
Read data by providing the key to the
read
method.Retrieve all keys and values stored in the bucket.
Access the latest changes in the storage to track modifications.
Helper class:
Use the
ReadWriteValue
class to simplify reading and writing values.This class allows easy access to values through getters and setters, reducing code verbosity.
Delete data:
Delete an entry using the
remove
method.Clear all data in the storage bucket using the
erase
method for a complete reset.
Listening to changes:
Listen to changes in storage to trigger actions such as logging, analytics, and UI updates.
Gain finer control by listening to changes for a specific key, allowing targeted responses to storage modifications.
GetxService
Need for GetxService:
Controllers in GetX do not work well for services that need to remain alive throughout the app’s life cycle, such as database, cache, or authentication services.
GetxService is designed to stay alive for the entire app’s life cycle, unlike regular controllers that may get disposed of, making it ideal for services that must be available throughout the app.
Initialization:
Create a subclass of
GetxService
for the desired service.Initialize necessary resources inside the
onInit
method.Inject the service in the
main
method to ensure it is available before the app starts.
Adding additional methods:
Organize code by placing all service-related methods in the GetxService subclass.
Add methods to read and write data models, providing a clean and organized approach to handling service logic.
Accessing the service:
Access the service throughout the app using
Get.find
, similar to how controllers are accessed in GetX.
Get hands-on with 1400+ tech skills courses.