How do I Group and List data according to date using Flutter, Firebase - Firestore Database?

152

This has already been answered here: Flutter/Dart how to groupBy list of maps

The package collection implements the groupBy function.

Share:
152
Gamer boy
Author by

Gamer boy

Updated on January 04, 2023

Comments

  • Gamer boy
    Gamer boy over 1 year

    I need to have a list of hotels grouped by the dates available (group by - the date they have been added). Please let me know what can be done. The following is my code. Thanks in advance.

                                        StreamBuilder(
                                          stream: FirebaseFirestore.instance
                                              .collection('hotels')
                                              .snapshots(),
                                          builder:
                                              (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                                            if (!snapshot.hasData) {
                                              return Center(child: CircularProgressIndicator());
                                            }
                                            return ListView.builder(
    
                                           
                                                itemCount: snapshot.data?.docs.length,
                                                itemBuilder: (context, index) {
                                                  return Stack(
                                                    children: [
    

    I want to have the date specified above each grouped list

    //the date captured from the group by condition
                                        Text("${example date}", style: TextStyle(
                                          color: Colors.white
                                        ),),
                                                      Container(
                                           
                                                        child: Column(
                                                        
                                                          children: [
                                                            Column(
                                                              crossAxisAlignment:
                                                              CrossAxisAlignment.end,
                                                              children: [
                                                                Padding(
                                                                  padding: const EdgeInsets.only(
                                                                      right: 10.0),
                                                                  child: Text(
                                                                    "${snapshot.data!.docs[index]['name']
                                                                        .toString()}",
                                                                    style: TextStyle(
                                                                      fontSize: 14,
                                                                      color: Colors.white,
                                                                    ),
                                                                  ),
                                                                ),
                                                             
                                                            
                                                              ],
                                                            ),
                              
                                              ],   ),   ), ],);});   },     ),