Riverpod 的 StreamProvider 只产生一次 StreamValue |Flutter & Hive



我编写了一个StreamProvider,在启动后立即收听,以获取有关潜在登录用户的所有信息。如果没有用户,那么结果将为null,侦听器将处于加载状态,所以我决定发回一个空用户的默认值,让我知道加载已经完成。我不得不这么做,因为Hive的watch((方法只有在数据更改时才会触发,而在启动时不会触发。因此,在那之后,我希望watch((方法完成它的工作,但问题是以下场景:

  1. 启动时:无用户-插入用户->则触发监视方法->我获取插入的用户数据->正在删除已登录的用户->watch方法未被触发。

  2. 启动时:完全用户-删除用户->则触发监视方法->我得到一个空用户->插入用户->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' : 'name@email.com',
'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((方法应该保持活动状态,即使已经发出了一个值吗?

但watch((方法在被触发后不再被触发一次

这是因为在每次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的同一个实例框,并且不要在每个实例框之后关闭该框,当您停止侦听streamlocalUsersBoxFutureProvider时,它将关闭自身

最新更新