Setting variable inside onDataChange in Firebase (singleValue Listener)

前端 未结 3 1258
孤街浪徒
孤街浪徒 2020-12-19 15:28

This is just an example of what my code is like. Right now, if I print out the userName variable inside onDataChange, it works fine. But if I try printing userName outside,

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 16:20

    This is something I've often found tricky with Firebase. The only real solutions I could suggest would be to make sure that any code where you need to access the Firebase variables is either:

    1) Inside the onDataChange method.

    2) Only called/accessed once the addListenerForSingleValueEvent onDataChange method has completed.

提交回复
热议问题