How to save data in my settings menu in Flutter?

1,339

You should use shared_preferences package. It's very simple and it's for non-critical data like this! Here is an example how to use it:

class SettingsRepository {
  final SharedPreferences preferences;

  SettingsRepositoryImpl(this.preferences);

  Future<int> getDifficulty() {
    return preferences.getInt(Keys.difficulty) ?? 1;
    // this 1 in the end is a default value
  }

  Future setDifficulty(int difficulty) {
    return preferences.setInt(Keys.difficulty, difficulty);
  }
}

To learn more go to https://pub.dev/packages/shared_preferences. I assume you want to call preferences.setBool

Share:
1,339
Icksir
Author by

Icksir

Updated on December 26, 2022

Comments

  • Icksir
    Icksir over 1 year

    I was wondering how to save data locally in Flutter. I am creating a Settings menu in my app, and I have a draft made of that UI, but I want to save the user's preferences when they close that menu, but I have no idea how to accomplish that.

    Do you know some tutorial to do so? I have searched in Youtube, but I have no idea how to search for it. I have only found UI tutorials and I don't want that.

    A picture of my UI is (I want to save that boolean option).

    enter image description here

    I would appreciate any help you could give to me!