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

Time:03-14

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

CodePudding user response:

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

The package collection implements the groupBy function.

  • Related