Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Firebase赢得';不要让我添加新字段_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java Firebase赢得';不要让我添加新字段

Java Firebase赢得';不要让我添加新字段,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我正在用java语言开发一个android应用程序,我正在使用firebase进行数据存储。它以前工作正常,但最近我在文档中添加了新字段。我通过快照侦听器获取该文档的实时更新。但是当我尝试从DocumentSnapshot对象获取新字段时,它返回null。仅适用于新字段。请注意,我没有直接在数据库中实际添加字段。我只修改了snapshot listener中的代码来获取这些字段。此外,文档最初并不存在,但doc.exists()返回true。 下面是快照侦听器的代码。我做错了什么 public

我正在用java语言开发一个android应用程序,我正在使用firebase进行数据存储。它以前工作正常,但最近我在文档中添加了新字段。我通过快照侦听器获取该文档的实时更新。但是当我尝试从
DocumentSnapshot
对象获取新字段时,它返回null。仅适用于新字段。请注意,我没有直接在数据库中实际添加字段。我只修改了snapshot listener中的代码来获取这些字段。此外,文档最初并不存在,但
doc.exists()
返回true。 下面是快照侦听器的代码。我做错了什么

public LiveData<List<Booking>> getBookingsDataWithIds(List<String> bookingIds){
    if(clientBookings.getValue()==null && bookingIds.size()>0){

       db.collection("Bookings").whereIn("Booking-ID",bookingIds).addSnapshotListener(new EventListener<QuerySnapshot>() {
           @Override
           public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
               if(error!=null){
                   Log.e("BOOKINGS ERROR: ",error.getMessage());
               }
               if(value!=null){
                   List<DocumentSnapshot> docs=value.getDocuments();
                   bookings=new ArrayList<>();
                   for(DocumentSnapshot doc : docs){
                       if(!doc.exists())
                           continue;
                       bookings.add(new Booking(doc.getId(),
                               doc.get("Status").toString(),
                               doc.get("Service Station").toString(),
                               doc.get("Client Name").toString(),
                               doc.get("Client Phone").toString(),
                               doc.get("Client Email").toString(),
                               (Double)doc.get("Lat"),
                               (Double)doc.get("Lng"),
                               doc.get("Vehicle Name").toString(),
                               doc.get("Vehicle Type").toString(),
                               doc.get("Date").toString(),
                               doc.get("Time").toString(),
                               doc.get("Admin Email").toString(),
                               doc.get("Payment Status").toString(),
                               doc.get("Payment Date").toString(),
                               doc.get("Payment Time").toString(),
                               (Integer) doc.get("Payment Charges"),
                               (Integer)doc.get("Payment Tip")));
                   }
                   clientBookings.postValue(bookings);
               }
               else{
                   Log.e("BOOKINGS ERROR:","value is null");
               }
           }
       });
    }
    return clientBookings;
}
public LiveData getBookingsDataWithIds(列出bookingIds){
if(clientBookings.getValue()==null&&bookingIds.size()>0){
db.collection(“Bookings”)。其中(“Booking ID”,bookingIds)。addSnapshotListener(new EventListener()){
@凌驾
public void onEvent(@Nullable QuerySnapshot值,@Nullable FirebaseFirestoreException错误){
if(错误!=null){
Log.e(“预订错误:,ERROR.getMessage());
}
if(值!=null){
列表文档=value.getDocuments();
预订=新建ArrayList();
用于(文档快照文档:文档){
如果(!doc.exists())
继续;
bookings.add(新预订)(doc.getId(),
doc.get(“Status”).toString(),
doc.get(“服务站”).toString(),
doc.get(“客户端名称”).toString(),
doc.get(“客户端电话”).toString(),
doc.get(“客户端电子邮件”).toString(),
(双)文件获取(“Lat”),
(双)文件获取(“Lng”),
doc.get(“车辆名称”).toString(),
doc.get(“车辆类型”).toString(),
doc.get(“日期”).toString(),
doc.get(“Time”).toString(),
doc.get(“管理电子邮件”).toString(),
doc.get(“付款状态”).toString(),
doc.get(“付款日期”).toString(),
doc.get(“付款时间”).toString(),
(整数)单据获取(“付款费用”),
(整数)单据获取(“付款提示”);
}
clientBookings.postValue(预订);
}
否则{
Log.e(“预订错误:,“值为空”);
}
}
});
}
返回客户预订;
}

因此,我通过删除代码来获取新添加的字段,从而解决了这个问题。然后我运行了一次应用程序,并重新添加代码来获取这些字段。例如,在我的代码中,假设字段“Payment Status”是我添加到firebase的新字段。所以现在我想去拿那块地。但一开始我不能这样做,因为它显示了我的错误,所以我删除了代码
doc.get(“付款状态”)
,并运行了该应用程序。然后我重新添加了这一行并再次运行,它运行得很好。

如果新属性之前不存在,我认为最好的方法是在尝试读取它们时始终检查空值。@AlexMamo是的,这实际上是一种更好的方法。虽然我解决了这个问题,但我最初在参数中传递null来代替新字段,并通过“Bookings”集合中的应用程序添加数据,然后将这些新字段重新添加到代码中:D。这很有效,但您的方法更简洁。谢谢很高兴听到这个消息。不客气;)@TahaKZed您介意用您找到的解决方案创建一个回复吗?@Roger抱歉回复太晚。我刚刚添加了解决方案作为对这个问题的回答。