Flutter 省道接收移除等待

Flutter 省道接收移除等待,flutter,dart,rxjs,rx-java,reactive-programming,Flutter,Dart,Rxjs,Rx Java,Reactive Programming,我现在正在学习rx/dart。但我正在挣扎。有没有办法使此属性同步?wait currentUser()强制我使属性异步。但是我需要一些技巧,因为我不能改变接口的这一部分。我不想使用适配器模式 属性应返回流。有没有办法做到这一点 Future<Stream<User>> get onUserOrAuthStateChanged async { return authentication.onAuthStateChanged .asyncMap<

我现在正在学习rx/dart。但我正在挣扎。有没有办法使此属性同步?
wait currentUser()
强制我使属性
异步。但是我需要一些技巧,因为我不能改变接口的这一部分。我不想使用适配器模式

属性应返回
。有没有办法做到这一点

Future<Stream<User>> get onUserOrAuthStateChanged async {
    return authentication.onAuthStateChanged
        .asyncMap<User>((authenticationUser) async {
      return await _retrieveUserFromAuthenticationUser(authenticationUser);
    }).concatWith([
      await currentUser() == null
          ? Stream.empty()
          : userRepository.getStream((await currentUser()).id).skip(1)
    ]);
  }

通过这种方法,我摆脱了异步,但是当执行
concatWith
方法时,currentAuthenticationUser总是
null
,因为赋值在
concatWith
之后发生不同步

您可以使用
Stream.fromFuture()
Future
转换为

这些是一些伪代码选项,因为我不完全了解您的代码,我也没有尝试编译它,我只是从内存中键入它

:userRepository.getStream((Stream.fromFuture(wait currentUser()).id)。跳过(1)
:userRepository.getStream((Stream.fromFuture(currentUser().map((u)=>u.id)))。跳过(1)

这是我提出的解决方案:

Stream<User> get onUserOrAuthStateChanged {
    return authentication.onAuthStateChanged.switchMap((authenticationUser) =>
        authenticationUser == null
            ? Stream.value(null)
            : userRepository.getStream(authenticationUser.uid));
  }
流获取OnUserRauthStateChanged{
返回authentication.onAuthStateChanged.switchMap((authenticationUser)=>
authenticationUser==null
?流值(空)
:userRepository.getStream(authenticationUser.uid));
}

或者,您可以使用新的
wait for
语法代替RxDart:

Stream get user$async*{
等待(firebaseAuth.onAuthStateChanged中的最终currentUser){
如果(currentUser!=null){
等待(getUserStreamFromId(currentUser.uid)中的最终用户){
从FireStore(用户)中生成UserProfile.fromFirestore;
}
}
}
}
我认为,
wait for
的工作原理类似于一个
switchMap
,我已经创建了一个测试程序来测试它

如果需要最新的值,可以让
提供程序
为您管理缓存

。。。
流提供者(
create:(context)=>userRepo.user$,
),
...
这是做这件事的最佳方式。然后,在孩子们中很容易访问它:

final user=context.watch();
...
更新

切换可能是一个更好的主意,因为dart
wait for
API会生成排气图:

Stream get user${
返回_firebaseAuth.onAuthStateChanged.switchMap((当前用户)异步*{
如果(currentUser!=null){
等待(getUserStreamFromId(currentUser.uid)中的最终用户){
从FireStore(用户)中生成UserProfile.fromFirestore;
}
}否则{
无效产量;
}
});
}

您将如何将此应用于上述场景?仍然不太明白“getter'id'没有为类'Stream'定义”。这不起作用。映射它以便获得
id
,我不知道您的类,所以我只是快速编写了一个伪代码。类似于
.map((u)=>u.id).跳过(1)
。或者在
currentUser
前面放置一个
wait
,如果这回答了您的问题,请不要忘记接受并投赞成票。
Stream<User> get onUserOrAuthStateChanged {
    return authentication.onAuthStateChanged.switchMap((authenticationUser) =>
        authenticationUser == null
            ? Stream.value(null)
            : userRepository.getStream(authenticationUser.uid));
  }