0

User data can be saved multiple ways with Flutter. In my case, I'm saving a simple List of objects. The objects have just a handful of String attributes. I load the data and convert it to a List of objects (from a Json string) when the main Scaffold() object is created.

First, when should data be saved? When the main Scaffold() object is being disposed? Every time the object list's state changes?

Second, every tutorial says use shared_preferences when you're saving a small amount of data locally and something like a local file when you've got more data (I don't need a database). In deciding between these methods, how much is a "small amount" of data? Is a Json string that's a few hundred characters long still a small amount? A few thousand characters?

Al C
  • 5,175
  • 6
  • 44
  • 74
  • Storing a couple characters? Use shared_preferences. Storing a novel? Use a local file. Where does the line between them lay? That's up to you. And as far as when, well, that's also up to you. Pick a time that feels "right". (Though don't depend on the main Scaffold being disposed as that happens when the app is closing, and Flutter doesn't have a reliable hook for when that happens so any data saving you start might not finish by the time the app closes and is garbage collected by the OS.) – Abion47 Jun 29 '21 at 23:14

1 Answers1

2
  • shared_preferences mostly used for storing short strings like name or username, or boolean but It's ok to save JSON as well even with 100 MB. It just saves files to your device as long as you have enough space. But your point is to keep data while you're not in that page anymore, right?

  • I use bloc to keep data in the inheritwidget, so all children can access that data. For example, register page, you might have 4-5 pages and need each data from each page to register. What I do is use bloc at the root, send data to that bloc and keep in memory, purse it after you finish using it

https://miro.medium.com/max/1400/1*8O6y2IQoxBTwan9TO1xG8Q.png

You can read more details here https://medium.com/@flutterhive/inheritedwidget-in-flutter-ffb64cc561dd

or else using bloc_provider https://pub.dev/packages/bloc_provider if you don't want to work with inheritwidget

A Flutter plugin to store data in secure storage:

Keychain is used for iOS AES encryption is used for Android. AES secret key is encrypted with RSA and RSA key is stored in KeyStore libsecret is used for Linux.

Einzeln
  • 517
  • 4
  • 14