
GetStorage stores key-value pairs in memory which backs up data to disk on each operation. Just add get_storage to your app and read from and write to containers, plus more.
A viewer requested me do a video on getx_storage so we're going to take a look at it in this video.
The pub.dev page for get_storage says that it's a "A fast, extra light and synchronous key-value in memory, which backs up data to disk at each operation." and that it is "not a database".
With that information I'm thinking it might be good for something simple, so for this example we're going to build a mock settings page.
Let's go ahead and start by installing the package. From your terminal window type this:
If everything worked correctly you should see it in your pubspec.yaml file in the root folder now.
We will now do our typical tutorial boilerplate, adding the get_storage import at the top.
Now inside main we will initialize GetStorage:
We will use async and await to wait for a response before continuing.
Now the absolute simplest example I can give is just calling read and write directly. So we will do a super simple example real quick. We will put this at the top of StorageApp:
And then in the appBar we will type something simple like this:
So this will attempt to read a key called example from GetStorage, and if it fails (e.g. it is null) it will just print"n/a".
Now let's add a button that when pressed will write a value to that example container:
And then when you run it the first thing you should see is the picture to the right.
BUT once you press the button, you should see this in your appBar:
So you can read from a key using .read and write to a key using .write, you can also remove a key by using .remove. So we will add a column and a second button:
And when you press the second button, it should return to "n/a".
So now we will do our real world example, a mock settings page. Let's start by setting up some default settings at the top:
And then let's go ahead and check to see if settings from GetStorage exists (aka not null). If it does we will use the settings from GetStorage. If it doesn't though, we will use the default settings:
This is what's called a Ternary Operator, or a simplified if/then statement. It simply says if data.read('settings') is not null, use it, otherwise use defaultSettings.
Now we will set up our listview. We will put it above our two buttons:
If everything worked correctly you should now see the list on your screen:
And then the last step we need to do is add a way to save the settings to GetStorage. We will just change our "Press Me" button:
So after restarting, you should be able to check one or more of the checkboxes, press the "Save Settings" button, and the restart again, and the values should still be there!
If you want a separate container for the settings page, you could set a specific container name in the init and GetStorage calls:
And then if you need to delete the container you can simply do this:
There are also a few additional entries I couldn't figure out how to work into my example, but I wanted to still touch on them. You can listen to changes to your container by doing something similar to this:
And you can listen to a specific key in your container by doing something similar to this:
And then if you want to stop listening you can do this:
I was thinking maybe you could use that to enable or disable your save button or something, but I'd probably handle that a different way. So I'm not exactly sure how to use that in this example. If you have ideas on how that can be used in this example let me know in the comments on the video above!
So that gives us a general idea of how to use get_storage in our application. Click here to view the full source.
Very long content goes here