0

I am attempting to combine two streams using RxDart and display the stream within a ListView.

I modeled this technique based on this post How do I join data from two Firestore collections in Flutter?, however, when I run this code, I am returning an empty dataset.

Any help would be greatly appreciated!

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"));
          }
        },
      ),
    );
  }
}

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'] ?? '');
  }
}
Jack
  • 49
  • 5
  • what is the value of `streams.length` inside `switchMap` callback? – pskink Jan 14 '20 at 06:43
  • Thanks for the reply! Hmm, suppose a validation isn't necessary there? I can go ahead and remove. Do you see any issues in the rest of the code? – Jack Jan 14 '20 at 15:22
  • i meanif it is > 0, so what is the value of `streams.length`? – pskink Jan 14 '20 at 15:30
  • It is meant to validate whether the count of streams == 0. Do you think that's the source of the issue? – Jack Jan 14 '20 at 15:55

0 Answers0