我需要将hivebd crud get、put、delete和update的所有操作分组到一个类中,但是当我尝试此操作时,会得到一个错误,因为hive.openbox()需要一个await,但构造函数不允许异步。我该如何做呢?
我的班级在下面。
class HomeListRepository {
var _homelistStore = await Hive.openBox<HomeList>("homelists");
Future insert(String id, HomeList homelist) async {
await _homelistStore.put(id,homelist.toMap());
}
Future update(HomeList homelist) async {
await _homelistStore.put(homelist.id,homelist.toMap());
}
Future delete(HomeList homelist) async {
await _homelistStore.delete(homelist.id);
}
Future<List<HomeList>> getAll() async {
return _homelistStore.values;
}
}
编辑:我解决了这个问题,最后的代码是:
class HomeListRepository {
Box _homelistStore;
Future<void> insert(String id,HomeList homelist) async {
_homelistStore = await Hive.openBox<HomeList>("homelists");
_homelistStore.put(id,homelist);
}
Future<void> update(HomeList homelist) async {
_homelistStore = await Hive.openBox<HomeList>("homelists");
await _homelistStore.put(homelist.id,homelist);
}
Future<void> delete(HomeList homelist) async {
_homelistStore = await Hive.openBox<HomeList>("homelists");
await _homelistStore.delete(homelist.id);
}
Future<List<HomeList>> readAll() async {
_homelistStore = await Hive.openBox<HomeList>("homelists");
return _homelistStore.values.toList();
}
}
在Hivegithub上发现page:github
暂无答案!
目前还没有任何答案,快来回答吧!