Search code examples
fluttersqlitesharedpreferencesflutter-secure-storage

Best way to use SharedPreferences, FlutterSecureStorage, etc


I want to create an app using Flutter that:

  1. Requires user to have a password to use the app. Is FlutterSecureStorage safe enough? I want to save the password encrypted. How to do this (best algorithm)?
  2. Shows the changes when opening/closing the app, options like theme the user chooses. Where and how should I save data? In SharedPreferences or a database like SQLite?
  3. I want to know the best way to pass values between pages. Do I save the values in the SharedPreferences, or do I pass them through the navigation URL?

Solution

    1. If there is no need to keep the password itself in the app for later use, the best thing would be to send it to some backend, through some secure protocol, e.g. https, that then would generate oauth tokens allowing you to validate the user without having anything to do with the password anymore (the safest way). But that is not always possible and the safe storage should be fine for those purposes, the phones also need to keep similar data secure so we can rather relay on them.
    2. SharedPreferences are totally fine for most of purposes. It's easy to use and quick enough. I would consider SQLite if I knew I will have to deal will huge amounts of structured data.
    3. It really depends on the actual use cases you have. If you want to pass session data, then SharedPreferences wouldn't be the best option and you should resort to some state management thingies, e.g. provider, bloc, ... I would resort StatefulWidgets only for keeping controllers and very minimalistic state that is strictly coupled with single Widget, and I would use permanent storage only for data that you want to preserve between sessions as it is slower and doesn't provide any ways to listen to it.