首页 > 解决方案 > StreamBuilder InitialData 和流控制

问题描述

这是 BLoc,它有我的 BehaviorSubject 和获取流和最新流值的方法。

  Stream<User> get currentUser => _currentUserSubject.stream;

  User get currentUserValue => _currentUserSubject.stream.value;

  final _currentUserSubject = BehaviorSubject<User>();

这是使用这些 currentUser 作为流源和 currentUserValue 作为 initialData 的 StreamBuilder:

StreamBuilder<User>(
        initialData: _loginAuthBloc.currentUserValue,
        stream: _loginAuthBloc.currentUser,
...)

当打开返回此 StreamBuilder 的页面时,我确保 currentUser 具有最新值。但是因为在订阅此流并返回最新值之前它有一些等待时间,所以我将 initialData 设置为 currentUserValue 以确保我们跳过该等待部分。到目前为止,一切都很好。但问题是我想忽略订阅流之后出现的值,并且在等待该流的最新值也到来之后,它与初始数据的值相同。你对此有什么建议吗?

标签: streamflutterbehaviorsubjectrxdartstream-builder

解决方案


推荐阅读