Riverpod 的 StreamProvider 在读取 Hive 的盒子时卡在加载中 |扑

问题描述

我正在尝试使用 Hive 将我保存的用户数据流式传输到名为“用户”的框中。这是用于根据用户提供的信息显示屏幕。目前,该框不包含任何数据,因此我希望以下代码显示蓝屏。否则它应该是绿色或紫色。我必须知道读取值何时完成,以便我知道返回值 null 是否表示数据尚未加载或用户框为空。

我正在使用 Riverpod 进行状态管理和这种方法。

我实现了以下两个提供者

final localUserBoxFutureProvider = FutureProvider<Box>((ref) async {
  final usersBox = await Hive.openBox('users');
  return usersBox;
});

final localUserStreamProvider = StreamProvider<User>((ref) async* {
  final usersBox = await ref.watch(localUserBoxFutureProvider.future);
  yield* usersBox.watch(key: 0).map((boxEvent) => boxEvent as User);
});

并希望像这样使用它们:

final localUserStream = watch(localUserStreamProvider);

return localUserStream.when(
  data: (data) => data == null ? Container(color: Colors.blue) : data.isEmailVerified ? Container(color: Colors.green) : Container(color: Colors.purple),loading: () => Container(color: Colors.yellow),error: (e,s) => Container(color: Colors.red)
);

这个实现的问题是它总是显示一个黄色的屏幕,这意味着它卡在加载中。有什么想法吗?

解决方法

嘿,我想我有一些解决方案,据我所知,Box 的 watch 方法在第一次运行时会为空,盒子是否有东西并不重要,因为 watch 只会在有东西时触发自它开始侦听的那一刻起发生了变化,因此它将处于加载状态,直到您更改应用中某处的键 0 值。

我不是很喜欢这种行为,如果 watch 方法第一次返回初始数据会更好

final localUserStream = watch(localUserStreamProvider);

return localUserStream.when(
  data: (data) => data == null ? Container(color: Colors.blue) : data.isEmailVerified ? Container(color: Colors.green) : Container(color: Colors.purple),loading: () => TextButton(
    onPressed: () async {
       final box = await watch(localUserBoxFutureProvider.future);
       await box.put(0,User()) // this is just an example that when you tap the button the stream actually change to data
    },child: Text('Update me'),),error: (e,s) => Container(color: Colors.red)
);

更新

这可能有点棘手(我还没有测试过),但您可以在 StreamProvider 中传输初始值

final localUserStreamProvider = StreamProvider<User>((ref) async* {
  final usersBox = await ref.watch(localUserBoxFutureProvider.future);
  yield* Stream.value(userBox.get(0,defaultValue: User())); //or getAt(0)
  yield* usersBox.watch(key: 0).map((boxEvent) => boxEvent as User);
});

这样,它会显示在应用程序开始时保存在您的框中的值,然后显示与该键相关的事件的更改

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...