4
votes

I am currently working on the chat aspect of my app. and I set up an AnimatedList inside of a StreamBuilder in order to make the messages appear in reverse. This is my code

      children: <Widget>[
        new Flexible(
          child: new StreamBuilder<QuerySnapshot> (
            stream: chatRoomRef.document(widget.chatRoomID).collection('messages')
                               .orderBy('time').snapshots(),
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
              return new AnimatedList(
                reverse: true,
                padding: const EdgeInsets.all(8.0),
                itemBuilder: (BuildContext context, int index, Animation<double> animation) {
                  return new ChatMessageListItem(
                    context: context,
                    index: index,
                    animation: animation,
                    reference: snapshot,
                  );
                }
              );
            }, 
          ),
        ),

My problem is that the builder is never hit, so the AnimatedList is never called. I am not sure the setup is correct so any help on this is much appreciated.

Edit: I am trying to make it work like the FirebaseAnimatedList widget. I dont know if that helps with understanding my goal here.

Thank you

2
where do you declare "chatRoomRef" ? - diegoveloper
I declare it in the State Class final CollectionReference chatRoomRef = Firestore.instance.collection('chatrooms'); - Romeo Bahoumda
AnimatedList, in your case, require to work the constructor parameter initialItemCount (it is indeed used in FirebaseAnimatedList: github.com/FirebaseExtended/flutterfire/blob/master/packages/… ). - user9776660

2 Answers

1
votes

Try to validate if your snapshot has data

            children: <Widget>[
                    new Flexible(
                      child: new StreamBuilder<QuerySnapshot> (
                        stream: chatRoomRef.document(widget.chatRoomID).collection('messages')
                                           .orderBy('time').snapshots(),
                        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                          return snapshot.hasData ? new AnimatedList(
                            reverse: true,
                            padding: const EdgeInsets.all(8.0),
                            itemBuilder: (BuildContext context, int index, Animation<double> animation) {
                              return new ChatMessageListItem(
                                context: context,
                                index: index,
                                animation: animation,
                                reference: snapshot,
                              );
                            }
                          ): new CircularProgressIndicator();
                        }, 
                      ),
                    ),
0
votes

Update: I fixed it by adding a custom animation as well as modifying the code in the documentation of cloud_firestore. My code looks like this now

 new Flexible(
              child: new StreamBuilder<QuerySnapshot> (
                stream: chatRoomRef.document(widget.chatRoomID).collection('messages')
                                   .orderBy('time').snapshots(),
                builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                  return snapshot.hasData ? new ListView(
                    physics: const AlwaysScrollableScrollPhysics(),
                    reverse: true,
                    padding: const EdgeInsets.all(8.0),
                    children: snapshot.data.documents.map((DocumentSnapshot messageSnapshot) {
                        return new ChatMessageListItem(
                        context: context,
                        animation: _animation,
                        messageSnapshot: messageSnapshot,
                        currentUserEmail: curUserEmail,
                      );
                    }).toList(),
                  ): const CircularProgressIndicator();