Can't find any information on this, because the answer already on here to use "listCollections()" or "getCollections()" are not even valid methods.
I have a database set up like so:
Layout: Collection -> Document -> Collection -> Documents
Example Data:
Stores -> UID -> StoreName1 ->
StoreName2 ->
StoreName3 ->
And so given that I know Stores and the UID, I should easily be able to return all the store names above. There has to be a line of code that will give me the results: StoreName1, StoreName2 & StoreName3.
Here is what I have:
CollectionReference documents = (await Firestore.instance.collection('Stores').document(widget.currentUserUID). <---- now what? There is no getCollections, there is no listCollections. How can I return this information?
Edit
So after discussing this with Peter below I have tried to work through an adjusted approach but it seems horrifically messy. Is there a better way of doing this? Feels wrong. Let me start with the new design:
Layout: Collection -> Document -> Collection -> Documents
Example Data:
Stores -> UID -> StoresList -> Store1
store_name: 'My Place'
Store2
store_name: 'Bellagio'
Store3
store_name: 'Grand Hotel'
So you can see that Store1, Store2 and Store3 above are documents and that they have fields, one of which is store_name.
I am trying to present the user with a list of the Stores they have under their account. The way I am having to do this, not knowing how many stores each user may want to operate, is as follows
storeCount == 0 ? SizedBox()
:
storeCount == 1 ? Container(
child: uid == null ? LoadingAnimation() : StreamBuilder(
stream: Firestore.instance.collection('stores').document(uid).collection('StoresList').document('Store1').snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return LoadingAnimationBasic();
}
if (snapshot.data.data == null) {
return LoadingAnimationBasic();
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (context, index) =>
_buildStoresList(context, snapshot.data),
);
}
},
)) :
storeCount == 2 ? Container(
child: uid == null ? LoadingAnimationBasic() : StreamBuilder(
stream: Firestore.instance.collection('stores').document(uid).collection('StoresList').document('Store1').snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return LoadingAnimationBasic();
}
if (snapshot.data.data == null) {
return LoadingAnimationBasic();
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (context, index) =>
_buildStoresList(context, snapshot.data),
);
}
},
),
Container(
child: uid == null ? LoadingAnimationBasic() : StreamBuilder(
stream: Firestore.instance.collection('stores').document(uid).collection('StoresList').document('Store2').snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return LoadingAnimationBasic();
}
if (snapshot.data.data == null) {
return LoadingAnimationBasic();
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (context, index) =>
_buildStoresList(context, snapshot.data),
);
}
},
),
) : SizedBox(),
And then the _buildStoresList function:
Widget _buildStoresList(BuildContext context, DocumentSnapshot document) {
storeName = document['store_name'];
return Center(
child: Text(
storeName,
style: TextStyle(
fontFamily: 'Petita',
color: Colors.black,
fontSize: 60,
),
),
);
}
So basically, first counting how many stores a user has (by counting the number of documents in the StoresList collection during initState() using a Future.delayed workaround to populate this correctly). Once I have the number of stores, the only way I can see to get the data from each document is to do that above with horrible nested if statements.
What have I got wrong here? Or is this the only way to do it?
I guess the main question is regarding this line:
stream: Firestore.instance.collection('stores').document(uid).collection('StoresList').document('Store1').snapshots(),
Where I need to give a specific document in order to snapshot the fields. It would be infinitely more efficient to have a line that took the snapshot of all documents in the collection and not just one at a time like this. Otherwise if the user wants to manage 200 stores, this 'if statement' nest is going to be insane.