Flutter 使用闭包的Dart过滤器流列表

Flutter 使用闭包的Dart过滤器流列表,flutter,dart,stream,Flutter,Dart,Stream,我有一个firestore记录列表作为流。我想将记录的uid传递给列表并返回下一项 uid name -----|------------- 001 | Steve 002 | David 003 | Mark 004 | George ------------------- uid名称 -----|------------- 001 |史蒂夫 002 |大卫 003 |标记 004 |乔治 ------------------- 如果我传递uid001,该方法应返回002 |

我有一个firestore记录列表作为流。我想将记录的uid传递给列表并返回下一项

uid name -----|------------- 001 | Steve 002 | David 003 | Mark 004 | George ------------------- uid名称 -----|------------- 001 |史蒂夫 002 |大卫 003 |标记 004 |乔治 ------------------- 如果我传递uid001,该方法应返回002 | David,传递001或004应返回null

class SearchResultsBloc implements BlocBase {
  SearchResultsBloc() {
    DatabaseService().Search().listen((data) => _inList.add(data));
  }

  final _listController = BehaviorSubject<List<Profile>>();
  Stream<List<Profile>> get outList => _listController.stream;
  Sink<List<Profile>> get _inList => _listController.sink;

  Stream<Profile> nextProfile(String uid) {
    var id = outList
        .map<int>(
            (results) => results.indexWhere((profile) => profile.uid == uid))
        .first;

    Stream<Profile> profile = id.then((index) {
      return outList.map<Profile>((results) => results.elementAt(index));
    });

    return profile;
  }


  @override
  void dispose() {
    _listController.close();
  }
}

class SearchResultsBloc实现BlocBase{
SearchResultsBloc(){
DatabaseService().Search().listen((数据)=>\u inList.add(数据));
}
final _listController=BehaviorSubject();
Stream get outList=>\u listController.Stream;
Sink get _inList=>_listController.Sink;
流nextProfile(字符串uid){
变量id=最外面的
.地图(
(results)=>results.indexWhere((profile)=>profile.uid==uid))
.首先;
流配置文件=id.then((索引){
返回outList.map((results)=>results.elementAt(index));
});
回报曲线;
}
@凌驾
无效处置(){
_listController.close();
}
}
我尝试过这个代码,但它抛出了错误

A value of type 'Future<Stream<Profile>>' can't be assigned to a variable of type 'Stream<Profile>'.
无法将“Future”类型的值分配给“Stream”类型的变量。

流nextProfile(字符串uid){
变量id=最外面的
.地图(
(results)=>results.indexWhere((profile)=>profile.uid==uid))
.首先;
流配置文件=id.then((索引){
返回outList.map((results)=>results.elementAt(index));
});
回报曲线;
}

好的,这似乎有效。不确定这是否是最佳解决方案

 Stream<Profile> nextProfile(String uid) {
    var index = outList
        .map<int>(
            (results) => results.indexWhere((profile) => profile.uid == uid))
        .first
        .asStream();

    int nextIndex;
    index.listen((index) {
      nextIndex = index + 1;
    });

    return outList.map<Profile>((results) => results.elementAt(nextIndex));
  }
Stream nextProfile(字符串uid){
var索引=最外层
.地图(
(results)=>results.indexWhere((profile)=>profile.uid==uid))
.首先
.asStream();
int nextIndex;
监听((索引){
nextIndex=指数+1;
});
返回outList.map((results)=>results.elementAt(nextIndex));
}

您需要
等待
id的调用。然后
。我需要将项目作为流返回。如果我使用wait,我将需要使该方法异步,这不适用于流。你别无选择,只能等待它,或者想出一个完全不同的解决方案。必须有一种不使用Future的方法来实现这一点。你正在使用streams。流本质上是异步结构。因此,首先调用
是一个异步操作,因此它返回未来。虽然可能有办法绕过这一点并同步获取数据,但它们几乎都是反模式,并导致脆弱、复杂和高度耦合的解决方案。相反,最好先问一下为什么需要同步获取这些数据,以及使用流是否适合此应用程序。
 Stream<Profile> nextProfile(String uid) {
    var index = outList
        .map<int>(
            (results) => results.indexWhere((profile) => profile.uid == uid))
        .first
        .asStream();

    int nextIndex;
    index.listen((index) {
      nextIndex = index + 1;
    });

    return outList.map<Profile>((results) => results.elementAt(nextIndex));
  }