How to get data from OnSuccessListener<QuerySnapshot>()

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "100";
    FirebaseFirestore db;

    String checkCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db= FirebaseFirestore.getInstance();

        db.collection("Users")
                .whereEqualTo("level", "admin")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        if (!queryDocumentSnapshots.isEmpty()) {
                            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
                            for (DocumentSnapshot d : list) {
                                checkCode = (String) d.get("kode");
                                Toast.makeText(MainActivity.this, "From inside : "+checkCode, Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    });
        Toast.makeText(MainActivity.this, "From outside : "+checkCode, Toast.LENGTH_LONG).show();
    }
}

I still confuse how to get data from cloud firestore.
So i tried making a simple code to get a data from there. I put 1111 code.

After i run the program, The toast from inside give me:

From inside : 1111 

But the toast from outside give me:

From outside : null

Why is that?
And how i can fix it?

4
Leave a Reply

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You are making an asynchronous call to Firestore. OnSuccessListener‘s onSuccess() method is called when data is retrieved, after you showed your Toast while checkCode is still null. You can move toast inside onSuccess() or just use Log to print to console.

Jason
Guest

The answer is simple, you declared the checkCode variable globally and it doesn’t have any value. so it will be null value and this value toasted there. But while OnSuccessListener executing its value will be assigned by the code checkCode = (String) d.get("kode");, so you get 1111 as its value.

Jason
Guest

The code inside onSuccess(QuerySnapshot queryDocumentSnapshots){} is an Asynchronous Callback see this answer for an explanation because the call to Firebasemay take some time to complete.
Because of this the line Toast.makeText(MainActivity.this, "From outside : "+checkCode, Toast.LENGTH_LONG).show(); will always execute before the callback does.

Jason
Guest

To transform QuerySnapshot to list of your objects use:

querySnapshot.toObjects(YourObject.class)

The line with Tost FromOutside is triggered before assigning value to the checkCode because the query to firebase is asynchronous. It is the reason that FromOutside is null.