Flutter Saving data locally, Shared Preferences versus Save to file

2,224

SharedPreferences are usually mostly for Settings etc. It will work most likely, but the performance will not be great (if that is a concern). Your Data will probably be stored just fine, but as it says on pub.dev, it's not guaranteed. The upside of SharedPreferences is that your Data will be available from anywhere in your App in an easily accessible way.

Saving to a file will also work, but you do have to remember that files you save in the AppDirectory are also not 100% safe from deletion. The Device OS can at any point clear these files. Again, probably not going to be an issue, but I thought i'd mention it.

Usually for on Device storage i would suggest sqflite (https://pub.dev/packages/sqflite). It's simple enough to use and has the best performance and "safety" for your Data.

Share:
2,224
alibiineed
Author by

alibiineed

Updated on December 20, 2022

Comments

  • alibiineed
    alibiineed over 1 year

    I am building a calendar app and for that I need to save the data locally. The data is in the form of a Map<DateTime,List<dynamic>>.

    I was thinking of using the SharedPreferences plugin for Flutter but on pub.dev it says "Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data." and so I'm hesitant to use it. Is there any advantage to storing data to SharedPreferences over just saving it to a file?

    I am not sure how exactly I'll save to a file but I'm thinking of converting it to a String using jsonDecode().