Skip to content
Snippets Groups Projects
Commit 81a8a73c authored by vjrj's avatar vjrj
Browse files

Cache init if box is null

parent 6c15e10f
No related branches found
No related tags found
No related merge requests found
......@@ -83,8 +83,6 @@ void main() async {
await HydratedStorage.build(storageDirectory: tmpDir);
}
ContactsCache().init();
// Reset hive during developing
if (!kReleaseMode) {
// Once.clearAll();
......
......@@ -22,7 +22,7 @@ class ContactsCache {
Box<dynamic>? _box;
Future<void> init() async {
Future<void> _init() async {
try {
if (kIsWeb) {
_box = await Hive.openBox(_boxName);
......@@ -49,7 +49,10 @@ class ContactsCache {
final String _boxName = 'contacts_cache';
Box<dynamic> _openBox() {
Future<Box<dynamic>> _openBox() async {
if (_box == null) {
await _init();
}
return _box!;
}
......@@ -128,7 +131,7 @@ class ContactsCache {
}
Future<void> storeContact(Contact contact) async {
final Box<dynamic> box = _openBox();
final Box<dynamic> box = await _openBox();
await box.put(contact.pubKey, <String, dynamic>{
'timestamp': DateTime.now().toIso8601String(),
'data': json.encode(contact.toJson()),
......@@ -136,7 +139,7 @@ class ContactsCache {
}
Future<Contact?> _retrieveContact(String pubKey) async {
final Box<dynamic> box = _openBox();
final Box<dynamic> box = await _openBox();
final dynamic record = box.get(pubKey);
if (record != null) {
......
......@@ -15,7 +15,7 @@ void main() {
lastChecked: DateTime(1970));
setUpAll(() {
ContactsCache().init();
// ContactsCache().init();
});
test('test put', () async {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment