Firebase value event listener not working

后端 未结 3 1106
梦谈多话
梦谈多话 2021-01-18 08:56

In my app, I have an id that is supposed to be pulled down from firebase real time database. If I pull it down and it sees there is not an id available, then it sets the id

3条回答
  •  误落风尘
    2021-01-18 09:34

    you need to set id = 1 into firebase database using setValue(), try below line of code it might help you

       userRef.child("id").
    
      addSingleValueEventListener(new ValueEventListener() {
    @Override public void onDataChange (DataSnapshot dataSnapshot){
        try {
            String value = dataSnapshot.getValue().toString();
            id = Integer.parseInt(value);
        } catch (NullPointerException e) {
             // if id=0 then set id=1 using setvalue()
            // this will set value of Id =1 in firebase database 
                 id=1;                 
            userRef.child("id").setValue(id);
            log.d(LOG_TAG, e.printStackTrace());
        }
    }
    
    @Override public void onCancelled (DatabaseError databaseError){
                id=1;
              userRef.child("id").setValue(id);
       }
    }); 
    

提交回复
热议问题