Firebase DataSnapshot.getValue() 为空
Firebase DataSnapshot.getValue() is null
我一直在查看 SO 中的其他错误,关于我的错误,但似乎仍然无处可去,因为我不断收到 DataSnapshot.getValue() 返回 null...
首先我得到了 db-ref:
private DatabaseReference f_database = FirebaseDatabase.getInstance().getReference().child("maps_data");
然后在 activity 我的 OnCreate 方法中添加了一个侦听器:
f_database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){ <<<< Problem is here, value is null
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
double lati = 0;
// Get UsersLocationFavorites object and use the values to update the UI
UsersLocationFavorites userLocFav = snapshot.getValue(UsersLocationFavorites.class);
LatLng location = new LatLng(userLocFav.getFavoriteSpot().getLatitude(), userLocFav.getFavoriteSpot().getLongitude());
gmap.addMarker(new MarkerOptions().position(location).title("Old Marker"));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
查看我的 firebase 数据库,我有以下内容:
我仔细检查了拼写 ("maps_data"),我在这里查看了很多 SO 问题,但我找不到适合我问题的问题。
谁能看出我做错了什么?
您混淆了 Firebase 提供的数据库。
您显示的所有代码都在访问 Firebase 实时数据库。但屏幕截图显示的是 Firestore 中的数据。这些是完全不同的数据库产品。如果您想从 Firestore 中读取数据,您应该改用 Firestore SDK。
我一直在查看 SO 中的其他错误,关于我的错误,但似乎仍然无处可去,因为我不断收到 DataSnapshot.getValue() 返回 null...
首先我得到了 db-ref:
private DatabaseReference f_database = FirebaseDatabase.getInstance().getReference().child("maps_data");
然后在 activity 我的 OnCreate 方法中添加了一个侦听器:
f_database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){ <<<< Problem is here, value is null
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
double lati = 0;
// Get UsersLocationFavorites object and use the values to update the UI
UsersLocationFavorites userLocFav = snapshot.getValue(UsersLocationFavorites.class);
LatLng location = new LatLng(userLocFav.getFavoriteSpot().getLatitude(), userLocFav.getFavoriteSpot().getLongitude());
gmap.addMarker(new MarkerOptions().position(location).title("Old Marker"));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
查看我的 firebase 数据库,我有以下内容:
我仔细检查了拼写 ("maps_data"),我在这里查看了很多 SO 问题,但我找不到适合我问题的问题。
谁能看出我做错了什么?
您混淆了 Firebase 提供的数据库。
您显示的所有代码都在访问 Firebase 实时数据库。但屏幕截图显示的是 Firestore 中的数据。这些是完全不同的数据库产品。如果您想从 Firestore 中读取数据,您应该改用 Firestore SDK。