Search code examples
stringflutterdartflutter-layoutflutter-dependencies

how to sort a string in array by recent add flutter


I have get an index here but I want display it from first to last in image : I want to display "hello" first then the restenter image
description here

code:

StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection("groups")
                  .doc(groupId)
                  .snapshots(),
              builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                var chats = snapshot.data?["chats"];
                return ...)};

               ListView.builder(
                              physics: const BouncingScrollPhysics(),
                              itemCount: chats.length,
                              shrinkWrap: true,
                              itemBuilder: (context, index) {
                                return  Text(chats[index]),
                                         
                              }),

Basically I want to display the recently added string first.


Solution

  • You have to reverse: ture in your listView, like this:

         ListView.builder(
         reverse:true,
                                  physics: const BouncingScrollPhysics(),
                                  itemCount: chats.length,
                                  shrinkWrap: true,
                                  itemBuilder: (context, index) {
                                    return  Text(chats[index]),
                                             
                                  }),