0

I new here, becuase I need some help with my code. I trying to make friend request in my app. In my case I want to save my requestid, which i just created in a string. I can do it and it works perfectly fine with documentReference.getId(). But now i want to use this ID in my second if-Statement as well, but it does not get there.

if (currentstate.equals("not_friends")) {
                final Map<String, String> request = new HashMap<>();
                request.put("yourid", yourid);
                request.put("otherid", otherid);
                request.put("Type", "req_send");

                fStore.collection("request").add(request).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        Toast.makeText(otherProfile.this, "Versendet", Toast.LENGTH_SHORT).show();
                        currentstate = "req_send";
                        add.setEnabled(true);
                        add.setText("Anfrage löschen");
                        String requestid = documentReference.getId();



                        Toast.makeText(otherProfile.this, requestid, Toast.LENGTH_SHORT).show();

                    }
                });

            }


            if (currentstate.equals("req_send")) {
                fStore.collection("request").document(requestid).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(otherProfile.this, "Gelöscht", Toast.LENGTH_SHORT).show();
                        add.setText("Freundschaftsanfrage versenden");
                        currentstate = "not_friends";
                        add.setEnabled(true);

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(otherProfile.this, "Nö", Toast.LENGTH_SHORT).show();


                    }
                });
            }

I add the data in the first If statement, but now the button changes and i want to delete the friend request in the second If Statement. So i want wo delete the data again in my Database. But the ID requestid does not save from the first statement, so I do not get acess the right data, which I wnt to delete.

LEON
  • 21
  • 9
  • Firestore loads data asynchronously, so there is no way you can simply use `requestid` outside the callback. Please check the duplicate to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Jan 30 '20 at 14:39
  • I pretty sure you are able to find out what id you are in and use it to update your data in your database – LEON Jan 30 '20 at 14:48

1 Answers1

0

Declare String requestid=null; outside the if statements i.e. global , then you will be able to access it in both the if blocks.Hope this works

  • i declared the String outside the if Statement as null. As a normals String tho. It doesnt work with a global either. Keep getting the Error, that the String is null – LEON Jan 30 '20 at 14:42