//It is in a loop
flag=false;
DataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot1) {
for (DataSnapshot snapshot2 : snapshot1.getChildren()) {
Book book = snapshot2.getValue(Book.class);
if((key).equals(book.getTable())){
flag=true;
}
}
//work correctly
System.out.println(flag);
}
@Override
public void onCancelled(@NonNull DatabaseError error1) {
}
});
//not work correctly
System.out.println(flag);
//end of loop
The variable flag; is not accessed outside the addValueEventListener as wanted.
Outside of the addValueEventListener, I want to access the variable.