Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

android - How to get new messages on bottom of the list view

When I'm fetching the messages from fire base new messages are appears randomly in the list view how I can set the new messages on the bottom of my list view in flutter?

Here is the code:

 body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            StreamBuilder<QuerySnapshot>(
              stream: _firestore.collection('messages').snapshots(),
              builder: (context, snapshot) {
                List<MyMessageContainer> messageWidget = [];
                if (!snapshot.hasData) {
                  return Center(
                    child: CircularProgressIndicator(
                      backgroundColor: Colors.lightBlue,
                    ),
                  );
                }
                final messagesDoc = snapshot.data.docs.reversed;
                for (var messageData in messagesDoc) {
                  final messageBody = messageData.data();
                  final messageText = messageBody['text'];
                  final messageSender = messageBody['sender'];
                  final msg = MyMessageContainer(
                    message: messageText,
                    sender: messageSender,
                    isCurrentUser: messageSender == user.email ? true : false,
                  );
                  messageWidget.add(msg);
                }
                return Expanded(
                  child: ListView(
                    reverse: true,
                    children: messageWidget,
                  ),
                );
              },
            ),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firestore has no built-in way to identify "new" messages. If you want that, you'll have to store a value in each document with a timestamp.

When you've done that, you can get them in the correct order in your code with:

_firestore.collection('messages').orderBy("timestampField").snapshots()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...