Home > front end >  how to sort a string in array by recent add flutter
how to sort a string in array by recent add flutter

Time:09-27

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.

CodePudding user response:

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]),
                                         
                              }),
  • Related