1

我正在尝试使用 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)
);

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

4

1 回答 1

1

嘿,我想我有一些解决方案,据我了解 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);
});

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

于 2021-01-09T17:28:27.850 回答