0

I am making an android application that requires users to input a city they live in on sign-up. Now, if user is already signed-on, I want the app to skip the main screen and jump to the map of the user's city. I managed to check if user is signed-on and skip the main screen, but I don't know how to properly fetch "city" variable.

I tried using switch case but it just crashes the app.

FirebaseAuth fAuth = FirebaseAuth.getInstance();
if(fAuth.getCurrentUser() != null){

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    FirebaseDatabase.getInstance().getReference(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            city = dataSnapshot.child("City").getValue().toString();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

    if(grad!=null) {
        switch (city) {

            case "city1":
                startActivity(new Intent(MainActivity.this, city1.class));
                break;
            case "city2":
                startActivity(new Intent(MainActivity.this, city2.class));
                break;
            case "city3":
                startActivity(new Intent(MainActivity.this, city3.class));
                break;
            default:
                break;
        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Amke
  • 1
  • If you understand Kotlin, then this [article](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5) will help for sure. – Alex Mamo Mar 09 '22 at 08:55

1 Answers1

1

Firebase works on an async thread so the data city gets filled later. You can solve this issue by create a function with city as the parameter and call it inside onDataChange() method.

something like this

FirebaseAuth fAuth = FirebaseAuth.getInstance();
        if(fAuth.getCurrentUser() != null){

            String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            FirebaseDatabase.getInstance().getReference(uid).addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    city = dataSnapshot.child("City").getValue().toString();
                   if(grad!=null){
                     yourfunc(city)
                   }
                
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            });


void yourfunc(string city){
 //your switch case
}
Mr.Code
  • 31
  • 4
  • Did it, now I get a new error on this line here `city = dataSnapshot.child("City").getValue().toString();` Error: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference – Amke Mar 09 '22 at 01:10
  • First of all it is a good practice to check if dataSnapshot is null or not and then ask for the data. Secondly, please make sure that the logged in user has "city" in his node. – Mr.Code Mar 09 '22 at 01:25