I have a utility class in my Android application used to hold functions that are reused throughout the app. One of the functions returns a Map of the current logged in users information. When I call this function from an Activity I get no errors but the Map appears to be empty.
Below is the utility class method
public static Map<String,String> getUserInfo(String uniqueID)
{
final Map<String, String> currUserInfo = new HashMap<>();
final DatabaseReference database =
FirebaseDatabase.getInstance().getReference().child("Users").child(uniqueID);
database.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
User user = dataSnapshot.getValue(User.class);
currUserInfo.put("user_firstname",user.getFirstName());
currUserInfo.put("user_secondname", user.getSecondName());
currUserInfo.put("user_username", user.getUsername());
currUserInfo.put("user_profile", user.getProfilePicURL());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
Log.d(TAG, "onCancelled: " + databaseError.getMessage());
}
});
return currUserInfo;
}
Below is how I am calling the function
userInfo = Utilities.getUserInfo(userID);
"userInfo" is declared at the top of the activity as this
private Map<String, String> userInfo;