Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my app, person A makes a post in a collection called "ADS". Person B clicks on the post and clicks on the button to send a request to person A. This creates a subcollection called "REQUESTS".

I want that in the case where person B clicks on the same post again and and if a request has already been sent to that particular post, a Text should appear that a request has already been sent.

Any help please?

What I have tried:

This is what I'm trying to do but I'm not sure if I'm right.

Dart
final uid = FirebaseAuth.instance.currentUser!.uid;
    
StreamBuilder(
                      stream: FirebaseFirestore.instance
                          .collection("ADS")
                          .doc(widget.selectedAds?['Uid'] ?? "")//the doc 
                          // id here is the id of the user who made the post. 
                          // User B selects that post and makes the request
                          .collection("REQUESTS")
                          .snapshots(),
                      builder: (context, snapshot) {
                        if (snapshot.connectionState == 
                        ConnectionState.done && snapshot.hasData) {
                          return ListView.builder(
                              // shrinkWrap: true,
                              itemCount: snapshot.data!.docs.length,
                              itemBuilder: (context, index) {
                                final passengerId =
                                    snapshot.data!.docs[index].get("UID"); //This is the id of the currentUser who sent the request
                                passengerId == uid
                                    ? Text(
                                        "Request already sent",
                                        style: TextStyle(color: Colors.white),
                                      )
                                    : Text(
                                        "Send a request",
                                        style: TextStyle(color: Colors.white),
                                      );
                                return null;
                              });
                        } else {
                          return Text(
                            "No data found",
                            style: TextStyle(color: Colors.white),
                          );
                        }
                      })


Can anyone help me? You can change the entire code if that will help me. Thank you!
Posted
Updated 28-Aug-23 9:30am
v2

1 solution

Your code is generally on the right track, I made small tweaks for you but as your code is now, you should be fine -
Dart
StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection("ADS")
      .doc(widget.selectedAds?['Uid'] ?? "")
      .collection("REQUESTS")
      .snapshots(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
      return ListView.builder(
        itemCount: snapshot.data!.docs.length,
        itemBuilder: (context, index) {
          final passengerId = snapshot.data!.docs[index].get("UID");
          if (passengerId == uid) {
            return Text(
              "Request already sent",
              style: TextStyle(color: Colors.white),
            );
          } else {
            return Text(
              "Send a request",
              style: TextStyle(color: Colors.white),
            );
          }
        },
      );
    } else {
      return Text(
        "No data found",
        style: TextStyle(color: Colors.white),
      );
    }
  },
)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900