Firebase实时数据库GetValue(POJO.class)未映射对象

时间:2018-03-23 12:24:26

标签: java android firebase firebase-realtime-database

我在POJO类中使用@PropertyName来映射firebase数据。更新到新版本12.0.0后。报告here@PropertyName不再有效了。

更新了POJO课程UserChatGroup.java

Firebase数据架构在这里user_chat_groups-> userIds->列表

GroupItem is here的数据架构

我正在使用ChildEventListener获取所有user_chat_groups

DatabaseReference mDatabaseRef =FirebaseDatabase.getInstance().getReference().child("user_chat_groups").child(userId);
mDatabaseRef.addChildEventListener(childEventListener);

 ChildEventListener childEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.exists()) {
                try {
  //Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
  //String groupName = (String) map.get("group_name");
  //String careproName = (String) map.get("carepro_name");
                    UserChatGroup chatGroup = dataSnapshot.getValue(UserChatGroup.class);
                    chatGroup.setKey(dataSnapshot.getKey());
                    completionHandler.onSuccess(chatGroup, FirebaseObserverType.CHILD_ADDED);
                } catch (Exception e) {
                    Log.d(TAG, "Exception " + e.getMessage());
                }
            } 
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

getValue返回null GroupMember member = dataSnapshot.getValue(GroupMember.class);

当我明确映射Object

时,它可以工作
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
String phone = (String) map.get("phone_number");
String name = (String) map.get("name");

当我尝试通过将所有firebase SDK版本更改为11.2.2(例如implementation 'com.google.firebase:firebase-database:11.2.2'时仍然编译为12.0.0

时,我会回到之前的Firebase版本

0 个答案:

没有答案