Get All Documents in Collection and Subcollection Firebase Firestore Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone assalamualaikum so here we'll take a  look and understand how to work with collections   documents sub Collections and documents now first  take a look at our Firebase console or backend   and while this is our Firebase console backend  here I have collections as you can see and we'll   focus on this collection over here now for this  collection we have some documents and if we click   each of these documents we do see that we have  different questions and information like that   well now each of these documents they also have a  sub collection so this is a collection documents   and this is my sub collection and inside this sub  collection I have documents as well and each of   them they contains collection and other fields and  if we click this sub collections we do see that   we have more documents and those documents also  have other documents or Fields like that actually   other fields not documents so we'll see how to  retrieve them and how to work with the query   from our floor app uh now one thing before we go  ahead with flutter app code over here I want you   to take a look so over here I do have this uh over  here list View and each of the list view actually   corresponds to this documents over here of course  this might not be that visible now if we click on   this over here so we will see that we have this  questions and a title like for example this title   is physics right now actually this is what it  refers to here like we do have physics and over   here we do see that we have like five questions  and a basic description okay and this is what we   see while we do see have five questions and basic  description now if we are going to click on this   we'll definitely see more documents over here  which are part of sub collections the documents   become part of sub Collections and as well as we  have uh question and answers and this exactly we   see if we click on this over here so we do see  that we have questions and answers of course we   can go ahead and click next next next and it will  work so this is the function actually that's got   called to load the data all this data as we click  on this it loads the data question answers so how   to work with this one well definitely we do have  like as I said we have inside our first collection   we have five documents okay now when we are on  this page actually we are sending a model over   here actually from the name you see that it's a  model now whenever you click on any of this card   it triggers calls this model and from this model  we send information so as we click on this our   models okay so we can refer to we refer to each  of these documents over here each of them over   here refers to this document so as we click on  this actually we are clicking on this documents   so as we click on this this function gets  called then we can access the ID so what we   are doing over here so first we are referring  to our Firebase collection with this and then   we refer to the documents with this collection  so over here this is the collection and these   are the documents as I said each documents they  have ID in our case these are the IDS so we get   the ID and for that ID we know that we have more  Collections and collections names are all same   over here so that's called questions so this is  our document and document ID and for the document   we get a sub collection so questions is our sub  collections so we get the sub Collections and   then we do dot get now with this dot get actually  we get all the documents that we have in our sub   collections so if this is our sub collection over  here we get a lot of documents as you can see so   when we are here when we're done with this  dot get we'll be actually getting all of this   anyway so whatever is that we get we save it  in a variable called question query of course   since we are getting data from Firebase  console back-end so we are returning Corey   snapshot so in our case we are returning  Corey's snapshot as a map string dynamic   so once we are done with this one actually we  are getting all these documents in this variable   in this variable okay now since we are returning  a map we can do more work on them so over here   we get all the talks we specially mentioned the  docs and then we call the map function and we   get a and we create a variable which is called  question and we pass to it and those questions   now at the same time we call questions from  Snapchat now what is this snapshot now this   step should you use like this now this snapshot  is uh more like Json that we get data now instead   of calling from Json we call it from snapshot but  you can call it anything so we pass each of these   questions all the documents so over here we're  passing the document document is our question   so we are passing them over here and we get  the document ID so this document ID would refer   to them then we have other fields we have seen  questions and question correct answer which refers   to question and correct answer okay so for that  sub collections we are accessing all the documents   and then we get question and correct answer  Fields over here question and correct answer   now we go back now we'll go back  to our quiz controller over here so   we get each of them and then we convert it to  an object and those objects are saved in a list   so at the end we convert this map to a list so  this one becomes a list and if this is a list   or not we can verify that now over here we'll  print a message since this is the list we can   access the first item in the list and we'll  print out this information as we click on   this card over here let's go ahead and do that  so let's click on this card so we see that over   here it says questions instance of questions  okay but we can dig dive more into this so   over here one thing you could do definitely  we could do here dot length so we could do   more things over here for example we can print  the ID over here let's go ahead and print ID we started we'll step back now we'll click on this  one more time and as we see this questions over   here being printed and we see that ID so now we'll  go ahead and click on the second one this one   and uh so if we scroll down to this and we  see exactly this is what the document under   the second sub collection this is what exactly  the document under the sub collection so it it   means that with this query over here so far now  which means that with this query and converting   it to this list of objects we can access up to  here and if we try to access more for example if   we want to access this answers collection we won't  be able to do that let's go ahead and check it now   as we can access ID over here now  we do like for example over here   we would do answers and let's see what we  get let's save it and then we'll step back and we'll click on the first one anyway now we  do see that it's empty so which means that with   this query we are done up to now so if we want to  access more items from our sub collection so the   question is a sub collection right subcollection  document and this is another sub collection so   which is not working so we need to have more query  now how to do that and actually I have done this   one now what's happening over here so I take this  question this question is already a list since   it's a list we can look through it so over here  this is a list okay now this list I'm assigning   to quiz paper DOT questions over here now that is  a model that model this field takes a list okay so   in this case this is the reason why I'm assigning  this list to this list okay now since this node is   a list I can access underlying objects from this  list inside this for Loop so this is why I have   the for Loop over here which is quiz paper DOT  questions so I'm taking the list and I'm getting   one items from the list each time as we go  through the list as we go through the for   Loop Okay now what's happening over here one  more time so first we get the collection and   for each collection we get the document ID  so once you are over here let me show you so when you do like this doc quiz paper.id  actually it refers to this ID okay great   then it means we can access up to here and after  that we do see that we have collections now this   collection refers to the collection name questions  which is this one okay great so so far we are able   to access this section over here and now we do  see that we have documents and questions dot ID   which refers to this document and document has  ID actually this is also the idea that we do see   that we have another collection that collection  is referring to this collections over here great   and after that what do we do we get all the  documents for this collections so which means   we are getting all of this now this one refers  to all the documents up to this node over here   now since this is going to return a map we want to convert them to a list so this is  what we do over here so over here we have a   snapshot from Json and over here at the same time  we convert some of these fields like as a string   identifier and answer so if we click on this  we see that identifier and answer so for this   document we get this fills and we convert them  to a string as well as return them as an object   as we do that we convert everything to a  list over here and save this in this variable   now this variable is assigned to the questions  over here which is this one anyway if you want   to work with the collections documents sub  collections document and Sub sub collections this   is the core part only thing you need to change  most probably based on your needs creating a model   anyway so part of the code is given in the link  below so you should check that out thank you
Info
Channel: dbestech
Views: 37,051
Rating: undefined out of 5
Keywords: Get All Documents in Collection and Subcollection Firestore Flutter, Firestore get all documents in subcollection, firestore get all documents in subcollection flutter, get data from subcollection firestore in flutter, firestore get subcollection from document, firebase, firestore, cloud firestore, How to get a Subcollection inside a Collection in, Firestore get nested collection, Flutter firestore get document field, firebase get all documents in collection
Id: MwZLVZKMFgo
Channel Id: undefined
Length: 12min 2sec (722 seconds)
Published: Mon Sep 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.