Firebase异步调用返回null
尝试添加无法正常运行的Log.d。 AnchorId是类级别的对象
String getValueFromDataBase(String shortCode){
databaseReference.child("CloudAnchorData").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "step 2" + anchorId.toString());
AnchorData usersData = dataSnapshot.child("CloudAnchorData").child(shortCode).getValue(AnchorData.class);
Log.d(TAG, "inside method" +usersData.toString());
if(usersData.getShortCode().equals(shortCode)){
anchorId = usersData.getCloudAnchorId().toString();
Log.d(TAG, anchorId.toString());
return;
}
Log.d(TAG, anchorId.toString());
Log.d("TAG", "Inside onDataChange() method!");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
Log.d("TAG", "After attaching the listener!" +anchorId.toString());
return anchorId;
}
应返回值,而不是null