“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

background

Today we introduce a lightweight tripartite Get_Stroage for data persistence, which can be used with get and can also help us synchronize data before multiple isolations

Get_stroage introduction

The simplest and most rude reason why we use get_stroage is that it is fast. All changes can be made in a second. Let’s see how get_stroage compares with other three parties

Looking at the data comparison, we can find that both reading and writing have very fast speed. As for why we can achieve such a fast speed, we will introduce it tomorrow. Today, we will first look at the use method

Add and delete

How to use get_stroage

First we need to initialize GetStorage inside main function, after initialization we can use normally

main() async {
  await GetStorage.init();
  runApp(App());
}
Copy the code

If called by instance, get the GetStorage instance first

final box = GetStorage();
Copy the code

Written information

box.write('quote', 'GetX is the best');
Copy the code

Read the information

print(box.read('quote'));
Copy the code

Delete the information

box.remove('quote');
Copy the code

You can also listen for all key-value pair changes, but of course you can remove that when you don’t need it anymore

Listen ((){print('box changed'); }); // Remove listener box.removelisten (listen);Copy the code

Of course, if you don’t want to listen to all of them and you only want to listen to one of them

box.listenKey('key', (value){
  print('new key is $value');
});
Copy the code

Clear all container data

box.erase();
Copy the code

When you want to create more than one container, you can do that too, remember the initialization method we used above, just pass in the parameters during initialization

await GetStorage.init('MyStorage');
Copy the code

Of course, get also provides a quick and convenient extension method. When the value of a member variable changes, you do not need to store the data again, because it is already stored for you, you just need to use it

class MyPref { static final _otherBox = () => GetStorage('MyPref'); final username = ''.val('username'); final age = 0.val('age'); final price = 1000.val('price', getBox: _otherBox); // or final username2 = ReadWriteValue('username', ''); final age2 = ReadWriteValue('age', 0); final price2 = ReadWriteValue('price', '', _otherBox); }... void updateAge() { final age = 0.val('age'); // or final age = ReadWriteValue('age', 0, () => box); // or final age = Get.find<MyPref>().age; age.val = 1; // will save to box final realAge = age.val; // will read from box }Copy the code

conclusion

Ok, today’s Get_Stroage use has been introduced, tomorrow look at the source code is how to achieve

I hope you can share some good three parties in the comments section, learn together and make progress together

As a student of Flutter, I hope you can give me more advice