skip to Main Content

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


  1. You need to use reference to retrieve the document in the future.

     var res = FirebaseFirestore.instance.collection("hotels");
     var docRef = res.doc(); // generates a new document reference with a unique ID
     var docId = docRef.id; // gets the ID of the new document reference    
     var data = {
      'hotel_name': data.hotelName,
      'hotel_email': data.email,
    };
    var response = await docRef.collection(data.email).doc(data.hotelName).collection(data.hotelName).doc(docId).set(data);
    

    Also to retrieve documents from the subcollection you should pass the document ID to the doc() method.

    final snapshot = await 
    res.doc(docId).collection(data.email).get();
    if (snapshot.docs.isNotEmpty) {
      snapshot.docs.forEach((doc) {
        print(doc.data());
      });
    } else {
      print('No documents found');
    }
    
    Login or Signup to reply.
  2. 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:

    final newHotel = <String, dynamic>{
    'hotel_name': data.hotelName,
    'hotel_email': data.email,
    };
    
     FirebaseFirestore.instance
                      .collection('hotels')
                      .doc(docId)           
                      .collection(data.email)
                      .doc(data.hotelName)
                      .collection(data.hotelName)
                      .doc()
                      .set(newHotel)
                      .onError((e, _) => print("Error writing document: $e"));  
    

    Retreive data using StreamBuilder:

     @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: StreamBuilder<QuerySnapshot>(
              stream: .collection('hotels')
                      .doc(docId)           
                      .collection(data.email)
                      .doc(data.hotelName)
                      .collection(data.hotelName)
                      .snapshots(), 
              builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return const Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const Text("Loading");
                }
                return ListView(
                    children: snapshot.data!.docs.map((DocumentSnapshot document) {
                  Map<String, dynamic> data =
                      document.data()! as Map<String, dynamic>;
                  return ListTile(
                    title: Text(data['hotel_name']), // 👈 You can access here
                    subTitle: Text(data['hotel_email']),
                  );
                }).toList());
              },
            ),
          ),
        );
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search