How get value from dataSnapshot?

前端 未结 2 1541
自闭症患者
自闭症患者 2020-12-19 13:55

When I debug this code, in databaseUser dataSnapshot has the values from User in database, but in the object user1, did not add, it\'s all null. And I don\'t un

相关标签:
2条回答
  • 2020-12-19 14:29

    I once faced that problem, and the solution was quite amazing.

    You should add setters in your User class.

    Try it, and you won't regret it.

    0 讨论(0)
  • 2020-12-19 14:44

    When you execute a query against the Firebase Database, there will potentially be multiple results. By attaching a ValueListener you get those results in one snapshot, so the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

    Your code fails to take this "list of results" into account. It's easy to fix by looping over the DataSnapshot.getChildren():

    databaseUser.orderByChild("userEmail").equalTo(user.getEmail()).addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
             User user1 = userSnapshot.getValue(User.class);
    
    0 讨论(0)
提交回复
热议问题