Skip to content
Advertisement

How to retrieve the data from firestore in maps

Is there any way to retrieve the data from firestore in Maps. I am trying but its returning null here is my code

JavaScript

But I am getting NullPointerException here Here is the screenshot of the database

strong text

I am not getting i why getdata function is returning null Here is the error log

JavaScript

Advertisement

Answer

You are getting null because the database query is asynchronous and returns immediately, before the query is complete. Your code continues to execute while the server performs the query, then the callback you provide to addOnSuccessListener is invoked some time later, whenever the query is complete.

As such, the line of code where you log the result is being executed before the query is complete and bus has the value you expect. If you add logging at the line immediately after getData, within the callback (not after the callback), you will see what I mean.

Your code will have to account for the asynchronous nature of the database calls. You won’t be able to use bus until after the query completes.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement