Flutter local_value Package: Simplest Way to Store Local Data

The easiest way to write values to local storage.

TL;DR: Here’s the pub.dev!

Around a year ago, I wrote this article, about the different ways you can write values to local storage. Even a year later, it’s one of my most active articles. A disproportionate amount of my views are on that article.

This means that Flutter devs are try to find the best way to store their files, but after working on many projects, it’s become clear that most devs aren’t looking to write any type of files to local storage — essentially, they’re serializing objects for use later. Things like current users, score data, cached API calls… These objects are stored the same exact way, every single time:

  1. write a toJson and fromJson method.
  2. determine in what local storage are you going to store the object (Shared Preferences? Application Documents Directory? Application Support Directory? Temporary?)
  3. write a method that writes the value to local storage after serialization
  4. write a method that reads that value and deserializes it into an object
  5. optionally write a delete method

If you got to step 6, congratulations! Now do it again for the next object :).

The other main issue is that path_provider doesn’t work on web — so if you port your methods over to web, you’re going to have to write a kIsWeb check. If you don’t go this route and do a general method, you end up with a type safety issue: You have to guarantee that what you write is exactly what you read, else your json conversions are going to throw.

All this to simply write to local storage! Highly repetitive work should be abstracted into a package.

There are times when you need to write highly structured data to disk. Anything at scale, or anything that needs to be highly structured or relational. This is not for that — I’d recommend looking at moor , which does a really good job being a local database for Flutter.

Instead, local_value is for object oriented data. Personally, I’ve already used my own package (dogfood!) in two applications.

  • To write Access Tokens to disk: I want these persisted over time, but I’m not as worried that they get leaked, like I do with my refresh tokens.
  • To write Notifications tree: Notifications in flutter takes a lot of management. You have to keep track of which notification to expire, which notifications come in and where they go, keep track of their metadata, etc. I write a JSON tree that allows me to nest notification data as deep as I’d like.
  • To write the current user: The current user has some data, like their name and ID. This is necessarily kept on the backend, but fetching it every time is a major pain and a waste. Instead, I write it to disk using local_singleton .
  • To keep a list: I have an application that serves as a federated learning model for color based chemical analysis (I actually know nothing about chemistry, I’m working with my schools chemistry department!). The user can pull models down from the cloud. I don’t want the user to have to re-pull the data every time, so I save it to disk using LocalValue!

I’m not going to go too in depth about how to use, but here’s a snippet from the readme:

//locally save a singleton:
final localCounterValue = LocalSingleton<int>(id: 'counter_val');
await localCounterValue.write(5);
print(await localCounterValue.read()); //5
//or save multiple related objects:
final localUserScores = LocalValue<int>(basePath: ['user_scores']);
await localUserScores.write('user_1', 15);
await localUserScores.write('user_2', 24);
print(await localUserScores.read('user_1')); //prints 15
print(await localUserScores.read('user_2')); //prints 24

Very simple API with a lot of flexibility, I think.

Don’t take my word for it, though: go try it yourself!

If you have suggestions / concerns, please let me know and I’m always open to learn. If you experience a bug or want to suggest a feature, head over to github issues to let me know!

I get absolutely no financial gain from this, nor do I ever intend to. I just wanted to write this to help people out.


Flutter local_value Package: Simplest Way to Store Local Data was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Anthony Oleinik

The easiest way to write values to local storage.

TL;DR: Here’s the pub.dev!

Around a year ago, I wrote this article, about the different ways you can write values to local storage. Even a year later, it’s one of my most active articles. A disproportionate amount of my views are on that article.

This means that Flutter devs are try to find the best way to store their files, but after working on many projects, it’s become clear that most devs aren’t looking to write any type of files to local storage — essentially, they’re serializing objects for use later. Things like current users, score data, cached API calls… These objects are stored the same exact way, every single time:

  1. write a toJson and fromJson method.
  2. determine in what local storage are you going to store the object (Shared Preferences? Application Documents Directory? Application Support Directory? Temporary?)
  3. write a method that writes the value to local storage after serialization
  4. write a method that reads that value and deserializes it into an object
  5. optionally write a delete method

If you got to step 6, congratulations! Now do it again for the next object :).

The other main issue is that path_provider doesn’t work on web — so if you port your methods over to web, you’re going to have to write a kIsWeb check. If you don’t go this route and do a general method, you end up with a type safety issue: You have to guarantee that what you write is exactly what you read, else your json conversions are going to throw.

All this to simply write to local storage! Highly repetitive work should be abstracted into a package.

There are times when you need to write highly structured data to disk. Anything at scale, or anything that needs to be highly structured or relational. This is not for that — I’d recommend looking at moor , which does a really good job being a local database for Flutter.

Instead, local_value is for object oriented data. Personally, I’ve already used my own package (dogfood!) in two applications.

  • To write Access Tokens to disk: I want these persisted over time, but I’m not as worried that they get leaked, like I do with my refresh tokens.
  • To write Notifications tree: Notifications in flutter takes a lot of management. You have to keep track of which notification to expire, which notifications come in and where they go, keep track of their metadata, etc. I write a JSON tree that allows me to nest notification data as deep as I’d like.
  • To write the current user: The current user has some data, like their name and ID. This is necessarily kept on the backend, but fetching it every time is a major pain and a waste. Instead, I write it to disk using local_singleton .
  • To keep a list: I have an application that serves as a federated learning model for color based chemical analysis (I actually know nothing about chemistry, I’m working with my schools chemistry department!). The user can pull models down from the cloud. I don’t want the user to have to re-pull the data every time, so I save it to disk using LocalValue!

I’m not going to go too in depth about how to use, but here’s a snippet from the readme:

//locally save a singleton:
final localCounterValue = LocalSingleton<int>(id: 'counter_val');
await localCounterValue.write(5);
print(await localCounterValue.read()); //5
//or save multiple related objects:
final localUserScores = LocalValue<int>(basePath: ['user_scores']);
await localUserScores.write('user_1', 15);
await localUserScores.write('user_2', 24);
print(await localUserScores.read('user_1')); //prints 15
print(await localUserScores.read('user_2')); //prints 24

Very simple API with a lot of flexibility, I think.

Don’t take my word for it, though: go try it yourself!

If you have suggestions / concerns, please let me know and I’m always open to learn. If you experience a bug or want to suggest a feature, head over to github issues to let me know!

I get absolutely no financial gain from this, nor do I ever intend to. I just wanted to write this to help people out.


Flutter local_value Package: Simplest Way to Store Local Data was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Anthony Oleinik


Print Share Comment Cite Upload Translate Updates
APA

Anthony Oleinik | Sciencx (2022-03-04T14:54:55+00:00) Flutter local_value Package: Simplest Way to Store Local Data. Retrieved from https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/

MLA
" » Flutter local_value Package: Simplest Way to Store Local Data." Anthony Oleinik | Sciencx - Friday March 4, 2022, https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/
HARVARD
Anthony Oleinik | Sciencx Friday March 4, 2022 » Flutter local_value Package: Simplest Way to Store Local Data., viewed ,<https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/>
VANCOUVER
Anthony Oleinik | Sciencx - » Flutter local_value Package: Simplest Way to Store Local Data. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/
CHICAGO
" » Flutter local_value Package: Simplest Way to Store Local Data." Anthony Oleinik | Sciencx - Accessed . https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/
IEEE
" » Flutter local_value Package: Simplest Way to Store Local Data." Anthony Oleinik | Sciencx [Online]. Available: https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/. [Accessed: ]
rf:citation
» Flutter local_value Package: Simplest Way to Store Local Data | Anthony Oleinik | Sciencx | https://www.scien.cx/2022/03/04/flutter-local_value-package-simplest-way-to-store-local-data/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.