public boolean cemail(final String email){
CollectionReference users = db.collection("Users");
Query query = users.whereEqualTo("email", email);
query.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot ds : task.getResult()){
if (ds.getString("email").equals(email)){
res = "asd";
x = false;
}
}
if (task.getResult().size() == 0){
res = "asd2";
x = true;
}
if (task.getResult().isEmpty()){
res = "asd3";
x = true;
}
}else {
if (task.getResult().size() == 0){
res = "asd2";
x = true;
}
if (task.getResult().isEmpty()){
res = "asd3";
x = true;
}
}
if (task.isCanceled()){
Toast.makeText(RegisterActivity.this, "Error:"+task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
Toast.makeText(this, "->"+res, Toast.LENGTH_SHORT).show();
return x;
}
我有这段代码,当我第一次调用该方法时,如果再次调用该方法可以正常返回正确的值,那么“ res”值为null,我的问题是为什么第一次返回null以及如何修复它。