首页 > 解决方案 > 类型转换错误从 Firebase 检索数据

问题描述

我目前在从 Firebase 检索ProductData类型产品的特定字段上的数据时遇到问题,这给了我投射错误。我想知道如何将 Firebase 映射转换为我的 ProductData,它是 Products.Below 是我的代码。 产品数据

class ProductData{
  String category;
  String description;
  String id;
  String images;
  bool isFeatured;
  bool isPopular;
  bool isPromotion;
  String name;
  double price;
  int quantity;

  ProductData({
    @required this.category,
    @required this.description,
    @required this.id,
    @required this.images,
    @required this.isFeatured,
    @required this.isPopular,
    @required this.isPromotion,
    @required this.name,
    @required this.price,
    @required this.quantity,

  });

  ProductData.fromDocument(DocumentSnapshot documentSnapshot){
    id=documentSnapshot.data()['id'];
    category= documentSnapshot.data()['category'];
    description= documentSnapshot.data()['description'];
    images= documentSnapshot.data()['images'];
    isFeatured = documentSnapshot.data()['isFeatured'];
    isPopular= documentSnapshot.data()['isPopular'];
    isPromotion= documentSnapshot.data()['isPromotion'];
    name = documentSnapshot.data()['name'];
    price = documentSnapshot.data()['price']+.0;
    quantity=documentSnapshot.data()['quantity'];
  }

Map<String, dynamic> toMap() => {
    "id":id,
    "category": category,
    "descripton": description,
    "imgUrl": images,
    "isFeatured":isFeatured,
    "isPopular":isPopular,
    "isPromotion":isPromotion,
    "name":name,
    "price":price,
    "quantity":quantity,
  };
  Map<String, dynamic> toResumeMap(){
    return {
      "id":id,
      "category": category,
      "descripton": description,
      "imgUrl": images,
      "isFeatured":isFeatured,
      "isPopular":isPopular,
      "isPromotion":isPromotion,
      "name":name,
      "price":price,
      "quantity":quantity,

    };
  }
  factory ProductData.fromMap(Map<String, dynamic> json) => ProductData(
    id: json["id"],
    name: json["name"],
    description: json["description"],
    price: json["price"]+0.0,
  );
}

问题出在购物车产品上,我在给我错误的行附近发表了评论。CartProduct.dart

class CartProduct {

  double totalPrice;
  String deliveryLocation;
  String userId;
  int quantity;
  double subtotal;
  String id;
  ProductData products;
  DateTime date;

  CartProduct(
  {
    //@required this.totalPrice,
    @required this.products,
    @required this.deliveryLocation,
    @required this.quantity,
    @required this.date,
    @required this.userId,
    @required this.id,
    @required this.subtotal
    }
  );


  CartProduct.fromDoucment(DocumentSnapshot document){

    date=document.data()['date'];
    deliveryLocation = document.data()['deliveryLocation'];
    id = document.data()['id'];
    products = document.data()['products']; // Here is where my issue is 
    quantity = document.data()['quantity'];
    subtotal= document.data()['subtotal'];
    userId=document.data()['userId'];
  }
  //Now we must also to Write the Data Into DB Based on our request.
  //Create a String Dynamic Function to write into the DB
  Map<String, dynamic> toMap(){
    return {
      //'totalPrice': totalPrice,
      'products': products.toResumeMap(),
      'quantity': quantity,
      'deliveryLocation': deliveryLocation,
      //'quantity': quantity,
      'userId': userId,
      'date': date,
      'id': id,
      'subtotal': subtotal
    };
  }
  Map<String, dynamic> toResumeMap(){
    return {
      "id":id,
      "category": products.category,
      "description": products.description,
      "imgUrl": products.images,
      "isFeatured":products.isFeatured,
      "isPopular":products.isPopular,
      "isPromotion":products.isPromotion,
      "name":products.name,
      "price":products.price,

    };
  }
  factory CartProduct.fromMap(Map<String, dynamic> json) => CartProduct(
     // totalPrice: json["totalPrice"],
      quantity: json["quantity"],
      deliveryLocation: json["deliveryLocation"],
      userId: json["userId"],
      products: json['products'],
      date: json["date"],
      id: json["id"],
      subtotal: json['subtotal']
  );
}

还可以在图片中找到我的 Firebase 字段。 在此处输入图像描述

提前致谢

标签: firebasefluttergoogle-cloud-firestore

解决方案


Helo,在您的 CartProduct 类中, products 变量是一个类(ProductData),但在您的方法中 CartProduct.fromDoucment 您直接在 products 变量中传递一些数据(您传递一个动态数据)。您应该在之前使用数据 (document.data()['products']) 初始化 ProductData。


推荐阅读