Search code examples
fluttergoogle-cloud-firestorecollections

flutter firebase: Access a collection


User 'A' makes a post in a collection called ADS. User 'B' makes a request to that post with a subcollection called Requests with a field Ad-Status: Pending approval. I want that after user 'B' makes the request, user 'A', the owner of the post, should be able to accept or reject the request by clicking on a button. On clicking the button, the subcollection Requests with the field Ad-Status should change to Approved. How do I do that?

Stream<QuerySnapshot> requestStream = FirebaseFirestore.instance
      .collection('ADS')
      .doc(FirebaseAuth.instance.currentUser!.uid)
      .collection('Requests')
      .snapshots();

StreamBuilder<QuerySnapshot>(
        stream: requestStream,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
final data = snapshot.data!.docs[index];
return Card(
child: ListTile(
title: Text(data.get('Ad-Status'))

TextButton(
onPressed: () {}, //I want the status in the database to change after pressing this button
child: Text('Accept')
)
)
)
}
}

Please I need help

Ads collection Request Subcollection


Solution

  • You can do something like this

    FirebaseFirestore.instance
        .collection('ADS')
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection('Requests')
        .doc(data.id)
        .set({'Ad-Status': 'Approved'}, SetOptions(merge: true));