Skip to content

Instantly share code, notes, and snippets.

@utkarsh-UK
Last active November 22, 2020 18:30
Show Gist options
  • Save utkarsh-UK/f8f3f11d3f97871f37476a6fadecb357 to your computer and use it in GitHub Desktop.
Save utkarsh-UK/f8f3f11d3f97871f37476a6fadecb357 to your computer and use it in GitHub Desktop.
Future<void> cacheUser(UserModel userModel) async {
try {
final CacheUserModel cacheUser = CacheUserModel(
userModel.email,
userModel.name,
userModel.phone,
userModel.role,
);
//CACHE_BOX_NAME is any string key
final userBox = await hive.openBox(CACHE_BOX_NAME);
await userBox.put('user', cacheUser);
await userBox.close();
} on Exception {
throw CacheException();
}
}
Future<CacheUserModel> getPersistedUser() async {
try {
final userBox = await hive.openBox(CACHE_BOX_NAME);
final CacheUserModel cacheUser = userBox.get('user') as CacheUserModel;
await userBox.close();
return cacheUser;
} on Exception {
throw CacheException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment