Skip to content

Instantly share code, notes, and snippets.

@egyleader
Created July 31, 2022 11:01
Show Gist options
  • Save egyleader/9bbd79084502766929cd99249c1a2269 to your computer and use it in GitHub Desktop.
Save egyleader/9bbd79084502766929cd99249c1a2269 to your computer and use it in GitHub Desktop.
quiet-marsh-7347
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