I have made a collection in this way:
var res = FirebaseFirestore.instance.collection("hotels");
var response =await res.doc(res.id).collection(data.email).doc(data.hotelName).collection(data.hotelName).doc().collection(‘hotel_details’).add( ‘hotel_name’: data.hotelName,
‘hotel_email’: data.email,});
This is the code to retrive I have used:
final res = FirebaseFirestore.instance.collection("hotels");
final response = await res.doc(res.id).collection(email).get();
I am getting empty list in response.docs
2
Answers
You need to use
reference
to retrieve the document in the future.Also to retrieve documents from the subcollection you should pass the
document ID
to thedoc()
method.Based on your code and making it the required corrections, to add and retrieve data you could possibly do it following way.
Add new hotel using
set
:Retreive data using
StreamBuilder
: