Dynamically Update ListView for 2 Events

The goal is to have a ListView that updates when either of these events happens:

  • Event A: The Firestore collection which provides data to the ListView changes.

  • Event B: The user selects a button in the app to sort the ListView by some attribute.

I know how to create a StreamBuilder to update the ListView for Event A, but I’m not sure how to update the ListView for Event B.

Is there something I can add inside the builder function of the StreamBuilder to listen for Event B and update the ListView when this happens?

For Event B, do you want to do sorting locally or would you like to do this via API?

Locally can be achieved using the sort function in dart. Sharing some snippets from my code below from my own implementation -

I had two values - Name and quantity as list headers.

  bool _sortByNameAsc = true;
  bool _sortByQuantityAsc = true;
  List<Product> searchProducts = [];

Tapping on which in turn did the sorting by controlling the boolean value and calling the sort function.

onTap: () {
  setState(() {
   _sortByNameAsc = !_sortByNameAsc;
 });
searchProducts.sort((a, b) {
 if (!_sortByNameAsc) {
   return a.name.compareTo(b.name);
 } else {
   return b.name.compareTo(a.name);
     }
   });
 _scrollToTop();
},

I also scrolled to top when the list was sorted. Similar functionality was implemented for sorting by quantity.

Hope this helps :slight_smile: Let us know here if you have any queries!

2 Likes

Hi Sakina,

Can i have your contact details, i want to get a flutter app developed

Raj

mam in some 10user details dynamic data storing how to move on next page in listview