将Firebase DB流与RxDart组合

将Firebase DB流与RxDart组合,firebase,flutter,firebase-realtime-database,Firebase,Flutter,Firebase Realtime Database,我试图使用RxDart组合两个流,并在ListView中显示该流 我根据这篇文章对这项技术进行了建模,但是,当我运行这段代码时,返回的是一个空数据集 任何帮助都将不胜感激 import 'package:rxdart/rxdart.dart'; class _HomeScreenState extends State<HomeScreen> { Stream<List<CombineStream>> _combineStream; String c

我试图使用RxDart组合两个流,并在ListView中显示该流

我根据这篇文章对这项技术进行了建模,但是,当我运行这段代码时,返回的是一个空数据集

任何帮助都将不胜感激

import 'package:rxdart/rxdart.dart';

class _HomeScreenState extends State<HomeScreen> {

  Stream<List<CombineStream>> _combineStream;
  String currentUserId = 'id12345'

  @override
  void initState() {
    super.initState();

      _combineStream =
          usersChatsRef.child(currentUserId).onValue.map((userChatStream) {
        return (userChatStream.snapshot.value).map((f) {
          Stream<UsersChats> uDetail =
              f.map<UsersChats>((node) => UsersChats.fromMap(node));

          Stream<Chat> sDetail = chatsDetailsRef
              .child(f.key)
              .onValue
              .map<Chat>((node2) => Chat.fromMap(node2.snapshot.value));

          return Rx.combineLatest2(
              uDetail, sDetail, (u, s) => CombineStream(u, s));
        });
      }).switchMap((streams) {
        return (streams.length) > 0
            ? streams.combineLatestList(streams)
            : streams.just([]);
      });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: StreamBuilder(
        stream: _combineStream,
        builder:
            (BuildContext context, AsyncSnapshot<List<CombineStream>> snap) {

          if (snap.hasData && !snap.hasError) {
            return ListView.builder(
              itemCount: snap.data.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(snap.data[index].combineUserChats.id),
                  subtitle: Text(myChats[index].combineUserChats.id),
                );
              },
            );
          } else {
            return Center(child: Text("No data"));
          }
        },
      ),
    );
  }
}
import'包:rxdart/rxdart.dart';
类_homescrenstate扩展状态{
流-组合流;
字符串currentUserId='id12345'
@凌驾
void initState(){
super.initState();
_联合团队=
usersChatsRef.child(currentUserId).onValue.map((userChatStream){
返回(userChatStream.snapshot.value).map((f){
流尾=
f、 map((node)=>UsersChats.fromMap(node));
Stream sDetail=chatsDetailsRef
.儿童(f.key)
.onValue
.map((node2)=>Chat.fromMap(node2.snapshot.value));
返回Rx.combineLatest2(
uDetail,sDetail,(u,s)=>CombineStream(u,s));
});
}).switchMap((流){
返回(流长度)>0
?流。组合相关测试列表(流)
:streams.just([]);
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
正文:StreamBuilder(
流:_combineStream,
建设者:
(BuildContext上下文,异步快照快照){
if(snap.hasData&!snap.hasError){
返回ListView.builder(
itemCount:snap.data.length,
itemBuilder:(上下文,索引){
返回列表块(
标题:文本(snap.data[index].combineUserChats.id),
字幕:文本(myChats[index].combineUserChats.id),
);
},
);
}否则{
返回中心(子项:文本(“无数据”));
}
},
),
);
}
}

类组合团队{
最终用户列表组合用户列表;
最后的聊天室;
CombineStream(this.combineUserChats,this.combineChat);
}
课堂聊天{
字符串id;
字符串名;
聊天({String id,String name}){
this.id=id;
this.name=名称;
}
factory Chat.fromMap(地图数据){
回话(
名称:数据['name']??'';
}
}
类UsersChats{
字符串id;
字符串更新;
用户沙茨(
{this.id,
此文件为.lastUpdate});
工厂用户schats.fromMap(映射条目数据){
返回UsersChats(
id:data.key???“”,
lastUpdate:data.value['lastUpdate']??'';
}
}

streams.length
inside
switchMap
callback的值是多少?感谢您的回复!嗯,假设不需要验证?我可以继续,并删除。您在代码的其余部分看到任何问题了吗?我的意思是,如果它大于0,那么
streams.length的值是多少?它用于验证流的计数是否=0。你认为这是问题的根源吗?

class CombineStream {
  final UsersChats combineUserChats;
  final Chat combineChat;

  CombineStream(this.combineUserChats, this.combineChat);
}

class Chat {

  String id;
  String name;

  Chat({String id, String name}) {
    this.id = id;
    this.name = name;
  }

  factory Chat.fromMap(Map data) {
    return Chat(
        name: data['name'] ?? '');
  }

}

class UsersChats {
  String id;
  String lastUpdate;

  UsersChats(
      {this.id,
      this.lastUpdate});

  factory UsersChats.fromMap(MapEntry<dynamic, dynamic> data) {
    return UsersChats(
        id: data.key ?? '',
        lastUpdate: data.value['lastUpdate'] ?? '');
  }
}