YouTube Description:

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:

flutter pub add get_storage

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.

import 'package:flutter/material.dart'; import 'package:get_storage/get_storage.dart'; void main() { runApp(StorageApp()); } class StorageApp extends StatefulWidget { @override _StorageAppState createState() => _StorageAppState(); } class _StorageAppState extends State<StorageApp> { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Get_Storage Example')), ), ); } }
 

Now inside main we will initialize GetStorage:

void main() async { await GetStorage.init(); runApp(StorageApp()); }

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:

class _StorageAppState extends State<StorageApp> { final data = GetStorage(); @override

And then in the appBar we will type something simple like this:

appBar: AppBar(title: Text('Get_Storage Example: ' + (data.read('example') ?? 'n/a'))),

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:

appBar: AppBar(title: Text('Get_Storage Example: ' + (data.read('example') ?? 'n/a'))), body: Center( child: ElevatedButton( child: Text('Press Me'), onPressed: () { setState(() { data.write('example','it works!'); }); }, ), // ElevatedButton ), // Center ), // Scaffold

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:

body: Center( child: Column( children: [ ElevatedButton( child: Text('Press Me'), onPressed: () { setState(() { data.write('example', 'it works!'); }); }, ), ElevatedButton( child: Text('Clear Me'), onPressed: () { setState(() { data.remove('example'); }); }, ), // ElevatedButton ], ), // Column ), // Center

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:

class _StorageAppState extends State<StorageApp> { final data = GetStorage(); Map<String, bool> defaultSettings = { "sound enabled": false, "music enabled": false, "show notifications": false, }; @override Widget build(BuildContext context) {

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:

Widget build(BuildContext context) { var settings = data.read('settings') ?? defaultSettings; return MaterialApp(

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:

children: [ ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: settings.length, itemBuilder: (BuildContext context, int index) { String key = settings.keys.elementAt(index); return CheckboxListTile( title: Text(key), value: settings[key], onChanged: (value) { setState(() { settings[key] = value!; }); }, ); // CheckboxListTile }), // ListView.builder ElevatedButton(

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:

}), // ListView.builder ElevatedButton( child: Text('Save Settings'), onPressed: () async { await data.write('settings', settings); setState(() { print("saved."); }); }, ), // ElevatedButton ElevatedButton( child: Text('Clear Me'),
 

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:

void main() async { await GetStorage.init('settingsContainer'); runApp(StorageApp()); }

class _StorageAppState extends State<StorageApp> { final data = GetStorage('settingsContainer'); Map<String, bool> defaultSettings = {

And then if you need to delete the container you can simply do this:

data.erase();
 

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:

data.listen(() { print("data saved."); });

And you can listen to a specific key in your container by doing something similar to this:

data.listenKey('settings', (value) { print("settings were saved."); });

And then if you want to stop listening you can do this:

data.removeListen(listen);

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.

 

More Videos