Skip to content

How would you create persist_shared_preferences example in signals package with state_beacon? #96

Answered by jinyus
Kypsis asked this question in Q&A
Discussion options

You must be logged in to vote

FWIW, this is how I'd architect it. This makes it easier to add/remove keys.

class Settings extends BeaconController {
  Settings(this.prefs);

  final SharedPreferences prefs;

  WritableBeacon<int> _intSetting(String key) {
    final b = B.writable<int>(prefs.getInt(key) ?? 0);
    b.subscribe((val) => prefs.setInt(key, val));
    return b;
  }

  WritableBeacon<String?> _stringSetting(String key) {
    final b = B.writable<String>(prefs.getString(key) ?? '');
    b.subscribe((val) => prefs.setString(key, val));
    return b;
  }

  WritableBeacon<bool> _boolSetting(String key) {
    final b = B.writable<bool>(prefs.getBool(key) ?? false);
    b.subscribe((val) => prefs.setBool(key, val…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Kypsis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants