且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android如何从Firebase数据库中删除价值?

更新时间:2022-06-03 03:21:21

dataSnapshot.getRef().setValue(null);不是删除Firebase对象的正确方法.您不允许将空值保存到数据库中.要删除它,请使用:

dataSnapshot.getRef().setValue(null); isn't the correct way of deleting your Firebase Object. You're not allowed to save a null value to your database. To remove it, you use:

dataSnapshot.getRef().removeValue();

您还应该检查该值是否不为null,因为您将删除该对象.

You should also check if the value isn't null, since you will delete that object.

if(snapshot.child("msg").getValue() != null){
   String msg = snapshot.child("msg").getValue().toString();
   return;
}