从 firebase firestore 检索数据列表时出错

Error retrieving a list of data from firebase firestore

我有一个购物应用程序,我需要在其中保存对象列表,保存到 firestorm 中工作正常,但在获取数据后,它不断崩溃并抛出错误,如果有人可以提供帮助,请提前致谢

  E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.shoppingapp, PID: 2110
    java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.shoppingapp.models.CheckoutModel
 
                Map<String,Object> paymentMap = new HashMap<>();
                paymentMap.put("total",String.valueOf(itemsTotal));
                paymentMap.put("items",list);

                firebaseFirestore
                        .collection("Orders")
                        .document(uniqueID)
                        .set(paymentMap)
 
   firebaseFirestore
                .collection("Orders")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        for(DocumentSnapshot document : task.getResult()){
                            List<CheckoutModel> map = (List<CheckoutModel>) document.get("items");
                            String totalPrice = document.get("total").toString();

                            // TESTING BY GETTING PRODUCT ID
                            // it throws exception here
                            textView.setText(map.get(0).getProductID());
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });

可以做两件事,

  1. 与其映射整个列表,不如尝试遍历文档并一次映射一个对象。
  2. 其次,在您的 checkOutModel 中为所有变量赋予初始值。如果变量没有初始值,则映射会出错。 例如,用“”实例化字符串变量,用 0 或 1 实例化数字变量。