【问题标题】:How to Fetch data from Firestore subcollection?如何从 Firestore 子集合中获取数据?
【发布时间】:2021-10-31 16:04:59
【问题描述】:

下面是产品集合的 Firebase Firestore 数据库,代码能够从集合中获取所有其他数据,但只有颜色字段返回 null,我正在使用 strembuilder 以及产品和颜色模型。

如果您知道我如何实现这一点,我将竭诚为您提供帮助。

产品型号

class Product with ChangeNotifier {
  String id;
  String title;
  String description;
  String price;
  String category;
  String currency;
  String condition;
  String firestoreid;
  String images;
  Seller seller;
  ProductColor colors;
  Offer offer;
  double rating;
  bool isFavourite;
  bool approved;

  Product({
    this.id,
    this.title,
    this.description,
    this.category,
    this.images,
    this.colors,
    this.rating,
    this.price,
    this.seller,
    this.currency,
    this.condition,
    this.firestoreid,
    this.approved,
    this.isFavourite,
    this.offer,
  });

  Product copy(
    String id,
    String title,
    String description,
    String price,
    String category,
    String currency,
    String condition,
    String firestoreid,
    String images,
    Seller seller,
    ProductColor colors,
    Offer offer,
    double rating,
    bool isFavourite,
    bool approved,
  ) =>
      Product(
        id: id ?? this.id,
        title: title ?? this.title,
        description: description ?? this.description,
        price: price ?? this.price,
        category: category ?? this.category,
        currency: currency ?? this.currency,
        condition: condition ?? this.condition,
        firestoreid: firestoreid ?? this.firestoreid,
        images: images ?? this.images,
        seller: seller ?? this.seller,
        colors: colors ?? this.colors,
        offer: offer ?? this.offer,
        rating: rating ?? this.rating,
        isFavourite: isFavourite ?? this.isFavourite,
        approved: approved ?? this.approved,
      );

  static Product fromJson(Map<String, dynamic> json) => Product(
        id: json['firestore_id'],
        title: json['productName'],
        description: json['productDescriptions'],
        category: json['categoryName'],
        images: json['image'],
        colors: ProductColor.fromJson(json['color']),
        rating: json[''],
        price: json['price'],
        seller: Seller.froJson(json['seller']),
        approved: json['approved'],
        currency: json['priceCurrency'],
        condition: json['productCondition'],
        firestoreid: json['firestore_id'],
      );
}

颜色模型

class ProductColor {
  String id;
  Color color;
  String qty;
  String size;

  ProductColor({
    this.id,
    this.color,
    this.qty,
    this.size,
  });

  static ProductColor fromJson(Map<String, dynamic> json) => ProductColor(
        id: json[''],
        color: json['color'],
        qty: json['qty'],
        size: json['size'],
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'color': color,
        'qty': qty,
        'size': size,
      };
}

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore model


    【解决方案1】:

    这里的问题是字段color 被保存为Array 内的Array。这意味着您使用的这段代码:

      static ProductColor fromJson(Map<String, dynamic> json) => ProductColor(
            id: json[''],
            color: json['color'],
            qty: json['qty'],
            size: json['size'],
          );
    

    不会工作,因为它需要 Object

    您可以更改保存数据的方式,使其保存为Object,或者更改fromJson,使其能够正确读取嵌套Array 的数据。

    【讨论】:

    • 请您建议我如何更改 'fromJson' 以读取嵌套的 'array' @tarik-huber
    • 您可以决定如何以及从数据库中提取哪些数据。正如@tarik-huber 所说,firestore 中的颜色字段包含数组和数组。所以 Product 对象也应该包含一个颜色数组。除非您只想提取数组第一个元素的颜色属性,在这种情况下您可以这样做:color: List.from(json['color']).first['color'] 或类似的东西。
    猜你喜欢
    • 2020-11-26
    • 2019-07-16
    • 2021-09-18
    • 2020-06-03
    • 2020-09-05
    • 2021-08-02
    • 2022-12-06
    • 2020-09-03
    • 2020-06-08
    相关资源
    最近更新 更多