Skip to content

Instantly share code, notes, and snippets.

@marcossevilla
Created May 4, 2021 17:56
Show Gist options
  • Save marcossevilla/b7b982ff771b2179018e95c175de8aa4 to your computer and use it in GitHub Desktop.
Save marcossevilla/b7b982ff771b2179018e95c175de8aa4 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:hive/hive.dart' show Box;
abstract class ILocalDataSource {
SomeModel getSomeModel();
Future<void> saveSomeModel(SomeModel model);
}
class LocalDataSource implements ILocalDataSource {
LocalDataSource({required Box box}) : _box = box;
final Box _box;
@override
SomeModel getSomeModel() {
final encodedJson = _box.get('model_key');
if (encodedJson != null) {
final model = SomeModel.fromJson(json.decode(jsonStr));
return model;
} else {
throw CacheException();
}
}
@override
Future<void> saveSomeModel(SomeModel model) async {
await _box.put('model_key', json.encode(model.toJson()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment