-
-
Save marcossevilla/b7b982ff771b2179018e95c175de8aa4 to your computer and use it in GitHub Desktop.
This file contains 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
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