无法从Firebase获取数据,它将返回一个空值



我正在尝试从Firebase检索数据:

DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference("Drivers").child(driverId).child("customerRideId");
Log.d("TAG_SiX",customerRef.toString());
customerRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// retriveCustomerID(dataSnapshot);
Log.d("TAG_eight",dataSnapshot.toString());
Map<String,String> map = 
(Map<String,String>)dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

这是日志猫值

D/TAg_five: DataSnapshot { key = customerRideId, value = null }

05

这是Firebase数据结构,我想要customerRideId的值,它在logcat中显示为null,并且customerRideId在DriverID中。

Drivers
PyltxOLXcWfZf6itgJEMWC4ylDu2
driverEmail: "nishant@gmail.com"
driverName: "Nishant"
customerRideId: "Sqpb6RFNhMOSiXeCrYj83aeeG7r2"

我想检索customerRideId的值,但在logcat中它显示空值,请帮助我。

here is the database screenshot

数据库屏幕截图在这里

请确保您已在 gradle 中添加了适当的依赖项,并且 Firebase 数据库中有一些数据 如果所有这些事情都很好,请尝试:

private FirebaseDatabase database;

在您的方法中:

database=FirebaseDatabase.getInstance();
DatabaseReference customerRef = database.getReference("Drivers/"+driverId+"/customerRideId");

试试这段代码..

DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference();
Query query1=customerRef.child("Drivers").child(customerRef.getKey()).child("customerRideId");
Log.d("TAG_SiX", customerRef.toString());
query1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// retriveCustomerID(dataSnapshot);
Log.d("TAG_eight", dataSnapshot.toString());
Map<String, String> map =
(Map<String, String>) dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

若要获取customerRideId属性的值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driverIdRef = rootRef.child("Drivers");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String customerRideId = dataSnapshot.child("customerRideId").getValue(String.class);
Log.d("TAG", customerRideId);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
driverIdRef.addListenerForSingleValueEvent(valueEventListener);

输出将为:Sqpb6RFNhMOSiXeCrYj83aeeG7r2

相关内容

  • 没有找到相关文章

最新更新