Created with <3 with dartpad.dev.
Created
July 31, 2022 11:01
-
-
Save egyleader/9bbd79084502766929cd99249c1a2269 to your computer and use it in GitHub Desktop.
quiet-marsh-7347
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Preferences { | |
static const _preferencesBox = '_preferencesBox'; | |
static const _counterKey = '_counterKey'; | |
final HiveInterface _hive; | |
const Preferences(this._hive); | |
Future<int> getCounter() => _getValue(_counterKey, defaultValue: 0); | |
Future<void> setCounter(int counter) => _setValue(_counterKey, counter); | |
Future<T> _getValue<T>(Object key, {required T defaultValue}) async { | |
final box = await _hive.openBox<Object>(_preferencesBox); | |
return box.get(key, defaultValue: defaultValue) as T; | |
} | |
Future<T?> _getValueOrNull<T>(Object key) async { | |
final box = await _hive.openBox<Object>(_preferencesBox); | |
return box.get(key) as T?; | |
} | |
Future<void> _setValue(String key, Object value) async { | |
final box = await _hive.openBox<Object>(_preferencesBox); | |
return box.put(key, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment