0

I have a ListView with comments items and every comment has a new ListView with comment reply, I've used Firebase and when I replied to comment it is showing the wrong text on ListView but in console correct text is printed. When I go back and come again it shows the correct value. Watch video for clarification.

 Padding(
          padding: EdgeInsets.only(left: 0.0),
             child: StreamBuilder(
             stream: getCommentReply(listMessage[index]['id']),
             builder: (context, snapshot) {
               if (!snapshot.hasData) {
               return Center(child: Text(''));
               }
               var listReplyMessage = snapshot.data.documents;
               return Padding(
                   padding: EdgeInsets.only(top: 8.0),
                     child: commentsReplyItems(listReplyMessage,
                     snapshot, listMessage[index]['id']));
        }))

  Widget commentsReplyItems(
    var listMessage, AsyncSnapshot snapshot, String commentID) {

       return CommentReplay(
       listMessage, snapshot, commentID, widget.currentUser, widget.postID);
 }




 Stream<QuerySnapshot> getCommentReply(String msgID) {
   Stream<QuerySnapshot> _query = Firestore.instance
    .collection('Posts')
    .document(widget.postID)
    .collection('Comments')
    .document(msgID)
    .collection('Reply')
    .orderBy('created_at', descending: true)
    .limit(2)
    .snapshots();

    return _query;
 }

Comment Reply Item

class CommentReplay extends StatefulWidget {
List<dynamic> listMessage;
AsyncSnapshot snapshot;
String commentID;
FirebaseUser currentUser;
String postID;

CommentReplay(this.listMessage, this.snapshot, this.commentID,
  this.currentUser, this.postID);

 @override
 _CommentReplayState createState() => _CommentReplayState();
}

class _CommentReplayState extends State<CommentReplay> {
  var _key = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
   return ListView.builder(
      shrinkWrap: true,
      key: _key,
      physics: ClampingScrollPhysics(),
      itemCount: widget.snapshot.data.documents.length,
    itemBuilder: (context, position) {
      //print(position);
      return Container(
        padding: EdgeInsets.only(top: 5.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
           Text(
                                  widget.listMessage[position['commentText'],
                                  textStyle: TextStyle(
                                      fontSize: 16.0,
                                      color: Color(0xff0D0E15)),
                                  textAlign: TextAlign.start,

                                )
          ],
        ),
      );
    });
}
7
  • 1
    I would suggest adding a code snippet of your own code. It's not good practice on StackOverFlow to redirect to a video for the reference code of the question... Commented Jan 14, 2020 at 7:24
  • In order for us to help, you must provide relavent code snippet. Commented Jan 14, 2020 at 7:30
  • I update my question Commented Jan 14, 2020 at 7:35
  • I am not sure this is going to fix, but try to add a Key to the container in the second itemBuilder (the one with replies). Commented Jan 14, 2020 at 10:32
  • Thanks but same result Commented Jan 14, 2020 at 11:08

1 Answer 1

5

after looking your code i find to use the key with comment reply Container solve your problem.

I used it like below and its working

 return ListView.builder(
    shrinkWrap: true,
    controller: _replayController,
    physics: ClampingScrollPhysics(),
    itemCount: snapshot.data.documents.length,
    itemBuilder: (context, index) {
      return Container(
        key: Key(listMessage[index]['id']),
        padding: EdgeInsets.only(top: 5.0),
        child: Row()
   )
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.