Here is my code. I am not able to access the username which is in signUp collection. I am getting the value but not accessible from outside.
So please tell me some m
Firebase APIs are asynchronous
, meaning that onDataChange()
method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because that method returns immediately, the value of your userName
variable you're trying to use it outside the onDataChange()
method, will not have been populated from the callback yet.
Basically, you're trying to return a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.
A quick solve for this problem would be to use the userName
string only inside the onDataChange()
method, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.