我编写了一个 StreamProvider,在启动后立即监听它以获取有关潜在登录用户的所有信息。如果没有用户,那么结果将为空,监听器保持在加载状态,因此我决定发回空用户的默认值,让我知道加载已完成。 我必须这样做,因为 Hive 的 watch() 方法仅在数据更改时触发,而在启动时则不会触发。 因此,在那之后,我希望 watch() 方法完成其工作,但问题在于以下场景:
启动时:无用户 - 插入用户 -> 触发监视方法 -> 获取插入的用户数据 -> 删除登录用户 -> 不触发监视方法.
启动时:完整用户 - 删除用户 -> 触发监视方法 -> 我得到一个空用户 -> 插入用户 -> 不触发监视方法。
一段时间后,我发现我可以根据需要经常使用所有 CRUD 操作,并且 Hive 的盒子会执行其应该执行的操作,但是 watch() 方法在触发后不再被触发 一次。
流媒体提供者:
final localUsersBoxFutureProvider = FutureProvider<Box>((ref) async {
final usersBox = await Hive.openBox('users');
return usersBox;
});
final localUserStreamProvider = StreamProvider<User>((ref) async* {
final usersBox = await ref.watch(localUsersBoxFutureProvider.future);
yield* Stream.value(usersBox.get(0, defaultValue: User()));
yield* usersBox.watch(key: 0).map((usersBoxEvent) {
return usersBoxEvent.value == null ? User() : usersBoxEvent.value as User;
});
});
听众:
return localUserStream.when(
data: (data) {
if (data.name == null) {
print('Emitted data is an empty user');
} else {
print('Emitted data is a full user');
}
return Container(color: Colors.blue, child: Center(child: Row(children: [
RawMaterialButton(
onPressed: () async {
final globalResponse = await globalDatabaseService.signup({
'email' : '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e202f232b0e2b232f2722602d2123" rel="noreferrer noopener nofollow">[email protected]</a>',
'password' : 'password',
'name' : 'My Name'
});
Map<String, dynamic> jsonString = jsonDecode(globalResponse.bodyString);
await localDatabaseService.insertUser(User.fromJSON(jsonString));
},
child: Text('Insert'),
),
RawMaterialButton(
onPressed: () async {
await localDatabaseService.removeUser();
},
child: Text('Delete'),
)
])));
},
loading: () {
return Container(color: Colors.yellow);
},
error: (e, s) {
return Container(color: Colors.red);
}
);
CRUD 方法:
Future<void> insertUser(User user) async {
Box usersBox = await Hive.openBox('users');
await usersBox.put(0, user);
await usersBox.close();
}
Future<User> readUser() async {
Box usersBox = await Hive.openBox('users');
User user = usersBox.get(0) as User;
await usersBox.close();
return user;
}
Future<void> removeUser() async {
Box usersBox = await Hive.openBox('users');
await usersBox.delete(0);
await usersBox.close();
}
知道如何告诉 StreamProvider watch() 方法应该保持事件状态,即使已经发出了一个值?
最佳答案
but the watch() method is not triggered anymore after it got triggered once
那是因为在每次 CRUD 之后您都会关闭该框,因此流(使用该框)停止发出值。如果您从 Riverpod 外部的某个地方调用它(await Hive.openBox('users')
),它调用相同的引用,这并不重要。仅当您停止使用它时才应该关闭该框,我建议使用带有 Riverpod 的 autodispose 在不再使用时关闭它,并且可能将这些 CRUD 方法放在由 Riverpod 控制的类中,这样您就可以完全控制该框的生命周期盒子
final localUsersBoxFutureProvider = FutureProvider.autoDispose<Box>((ref) async {
final usersBox = await Hive.openBox('users');
ref.onDispose(() async => await usersBox?.close()); //this will close the box automatically when the provider is no longer used
return usersBox;
});
final localUserStreamProvider = StreamProvider.autoDispose<User>((ref) async* {
final usersBox = await ref.watch(localUsersBoxFutureProvider.future);
yield* Stream.value(usersBox.get(0, defaultValue: User()) as User);
yield* usersBox.watch(key: 0).map((usersBoxEvent) {
return usersBoxEvent.value == null ? User() : usersBoxEvent.value as User;
});
});
并且在您的方法中使用 localUsersBoxFutureProvider 中的相同实例框,并且当您停止监听 stream
或 localUsersBoxFutureProvider
时,不要在每个实例框后关闭该框会自行关闭
关于flutter - Riverpod 的 StreamProvider 仅产生一次 StreamValue | flutter 和 hive ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65670416/