How to Fetch data from Firestore subcollection?

115

The Problem here is that the Field color is saved as an Array inside of an Array. That means that this code you use:

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

won't work because it expects an Object.

You can either change how you save the data so it gets saved as an Object or change the fromJson in a way that it reads the data correctly form that nested Array.

Share:
115
Oletech
Author by

Oletech

I'm full stack mobile/web app developer i always focus on create apps which help an organization to promote their brand, improve customer satisfaction, generate revenue not only that but also find new business opportunities.

Updated on December 31, 2022

Comments

  • Oletech
    Oletech over 1 year

    Below is the Firebase Firestore database for collections of products, the code was able to fetch all other data from the collection but only color field return null, i'm using strembuilder along sidewith product and color model.

    Please if you know how i can implement this, i will be great full for your help.

    Below is the Firebase Firestore database for collections of products, the code was able to fetch all other data from the collection but only color fiel return null

    Product Model

    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'],
          );
    }
    

    Color Model

    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,
          };
    }
    
  • Oletech
    Oletech over 2 years
    please your suggestion on how i can change 'fromJson' in the way it reads the nested 'array' @tarik-huber
  • Louis Deveseleer
    Louis Deveseleer over 2 years
    It's up to you to decide how and what data you want to extract from the DB. As @tarik-huber said the color field in firestore contains and array of arrays. So the Product object should contain an array of arrays for color as well. Unless you just want to extract the color property of the first element of the array, in which case you could do : color: List.from(json['color']).first['color'] or something like that.