我第一次在Flutter中使用Hive时遇到了一些问题。在此之前,我使用的是Sqflite,一切都如预期,但我切换到Hive,以看起来更具性能。我切换到了Hive。问题是当我初始化两个Hive盒"时;AvatarsPack";以及";AvatarItem";对于数据,即使等待DB上的每次插入,看起来Hive在所有插入完成之前都会返回,所以我的FutureBuilder在没有得到值的情况下进行构建。我试着解决这个问题大约3天了,我几乎要换回Sqflite了,但我记得这里有很多了不起的人可以在几秒钟内解决这个问题,所以这就是我正在做的,我请求你们所有人的帮助。感谢您阅读到这里,下面是代码:
avatars_view.dart
final _controller = AvatarsController();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _controller.initializeRepository(),
builder: (context, snapshot) {
Future.delayed(Duration(seconds: 1)).then((value) => print(_controller.getAvatarsPacks()[9].avatars));
if (snapshot.connectionState == ConnectionState.done) {
print('@@@@@${_controller.getAvatarsPacks()[0].avatars}');
return MyShop(
getOwnedItems: _controller.getOwnedAvatars,
ownedTabTile: 'Owned',
isScrollable: true,
headers: _controller.getHeaders(),
bodies:
_controller.getAvatarsPacks().map((e) => e.avatars).toList(),
);
}
return Container();
});
}
}
avatars_controller.dart
final _repository = AvatarsRepository();
final Map<String, int> _AVATARS_PACKS_AMOUNT = {
'superheroes': 50,
'children': 30,
'halloween-1': 50,
'halloween-2': 50,
'halloween-3': 50,
'hospital': 18,
'people-1': 50,
'people-2': 50,
'people-3': 18,
'professions': 50,
};
List<AvatarsPack> getAvatarsPacks() {
return _repository.getAllAvatarsPacks();
}
Future<void> initializeRepository() async {
await _repository
.openBoxes()
.then((value) => print('----Boxes are oppened'));
await _clearBoxes().then((value) => print('----Boxes were cleared'));
if (_repository.isEmpty()) {
print('----boxes are empty');
await _initializeRepositoryData().then((value) =>
print('avatars quantity: ${_repository.getQuantityAvatarItems()}'));
}
return;
}
List<String> getHeaders() {
return _AVATARS_PACKS_AMOUNT.keys.toList();
}
List<AvatarItem> getOwnedAvatars() {
return _repository.getOwnedAvatars();
}
void updateAvatarItem(AvatarItem avatarItem) =>
_repository.updateAvatarItem(avatarItem);
Future<void> _initializeRepositoryData() async {
print('*******_initializeRepositoryData called()');
return _AVATARS_PACKS_AMOUNT.forEach((key, quantity) async {
final avatarsPack = AvatarsPack.create(
packID: key,
quantity: quantity,
packItemPrice: Money.all(coins: 50, diamonds: 5));
await _repository.addPack(avatarsPack);
});
}
Future<void> _clearBoxes() {
return _repository.clearBoxes();
}
@override
FutureOr onClose() {
_repository.closeBoxes();
super.onClose();
}
}
avatars_pository.dart
Box _avatarsPackBox;
Box _avatarItemBox;
final AVATAR_PACK = 'avatarsPack';
final AVATAR_ITEM = 'avatarItem';
Box get _avatarsPackInstance {
return _avatarsPackBox ??= Hive.box(AVATAR_PACK);
}
Box get _avatarItemInstance {
return _avatarItemBox ??= Hive.box(AVATAR_ITEM);
}
Future<void> addPack(AvatarsPack avatarsPack) async {
await _avatarsPackInstance.add(avatarsPack.toJson());
return avatarsPack.avatars.forEach((element) async {
await _avatarItemInstance.add(element.toJson());
});
}
List<AvatarsPack> getAllAvatarsPacks() {
final avatarsPacks = _avatarsPackInstance.values
.map((json) => AvatarsPack.fromJson(json))
.toList();
avatarsPacks.forEach((avatarPack) {
avatarPack.avatars = getAvatarItemsFromPack(avatarPack.packID);
});
return avatarsPacks;
}
Future<void> addAvatarItem(AvatarItem avatarItem) async {
await _avatarItemInstance.add(avatarItem.toJson());
return;
}
Future<void> updateAvatarItem(AvatarItem avatarItem) {
final index =
getAllAvatarItems().indexWhere((e) => e.pack == avatarItem.pack);
return _avatarItemInstance.putAt(index, avatarItem.toJson());
}
List<AvatarItem> getAllAvatarItems() {
return _avatarItemInstance.values
.map((json) => AvatarItem.fromJson(json))
.toList();
}
int getQuantityAvatarItems() {
return _avatarItemInstance.values.length;
}
List<AvatarItem> getAvatarItemsFromPack(String packID) {
final List<AvatarItem> avatars = [];
getAllAvatarItems().forEach((avatarItem) {
if (avatarItem.pack == packID) avatars.add(avatarItem);
});
return avatars;
}
List<AvatarItem> getOwnedAvatars() {
return getAllAvatarItems().where((element) =>
element.isOwned()).toList();
}
Future<void> openBoxes() async {
await Hive.openBox(AVATAR_PACK);
await Hive.openBox(AVATAR_ITEM);
return;
}
Future<void> closeBoxes() async {
await _avatarsPackInstance.close();
await _avatarItemInstance.close();
return;
}
Future<void> clearBoxes() async {
await _avatarsPackInstance.clear();
await _avatarItemInstance.clear();
return;
}
bool isEmpty() {
return _avatarsPackInstance.isEmpty;
}
}
关于我的化身图像,它们位于名称与其packID相同的文件夹中,并且每个文件夹包含以这种方式命名的X个元素img-0、img-2、。。。,img-[x-1],img-x。任何反馈都将不胜感激,谢谢:(
尝试使用ValueListenableBuilder((而不是FutureBuilder((。这会观察长方体上的变化并相应地反映这些变化。
import 'package:hive_flutter/hive_flutter.dart';
ValueListenableBuilder(
valueListenable:
Hive.box(YOURBOX).listenable(),
builder: (context, Box box, _) {
return SomeWidget(withSomeData);
},