Flutter - Fetch data from firestore sub collection using streambuilder

Viewed 67

Product Model

Product model used in provider

class Product with ChangeNotifier {
      String id;
      String title;
      String description;
      String price;
      String category;
      String currency;
      String condition;
      String firestoreid;
      String images;
      List<Seller> seller;
      List<ProductColor> colors;
      List<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,
        List<Seller> seller,
        List<ProductColor> colors,
        List<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: json[''],
            rating: json[''],
            price: json['price'],
            seller: List<Seller>.from(
              json['seller'].map(
                (orderProdct) => Seller(
                  id: orderProdct['id'],
                  name: orderProdct['name'],
                  type: orderProdct['type'],
                ),
              ),
            ),
            approved: json['approved'],
            currency: json['priceCurrency'],
            condition: json['productCondition'],
            firestoreid: json['firestore_id'],
          );
    }

Seller Model

Seller Model called inSide Product Model as list

class Seller {
  String id;
  String name;
  String type;

  Seller({
    this.id,
    this.name,
    this.type,
  });

  Seller copy(
    String id,
    String name,
    String type,
  ) =>
      Seller(
        id: id ?? this.id,
        name: name ?? this.name,
        type: type ?? this.type,
      );

  static Seller froJson(Map<String, dynamic> json) => Seller(
        id: json['id'],
        name: json['name'],
        type: json['type'],
      );
}

screenshoot from firestore db Product collection

firestore db Product collection

screenshoot from firestore db Product sub-collection

Product color sub-collection

Debug Console Message

I/flutter (13089): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Seller>'
W/e.kariakoonlin(13089): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (light greylist, linking)
Reloaded 45 of 1268 libraries in 1,910ms.
I/flutter (13089): type '(dynamic) => Seller' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
Reloaded 45 of 1268 libraries in 2,233ms.
I/flutter (13089): type '(dynamic) => Seller' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

Read product from firestore

static Stream<List<KariakooProduct>> getProduct() =>
      FirebaseFirestore.instance
          .collection('products')
          .snapshots()
          .transform(Utils.transformer(KariakooProduct.fromJson));

Transformer

class Utils {

  static StreamTransformer transformer<T>(
          T Function(Map<String, dynamic> json) fromJson) =>
      StreamTransformer<QuerySnapshot, List<T>>.fromHandlers(
        handleData: (QuerySnapshot data, EventSink<List<T>> sink) {
          final snaps = data.docs.map((doc) => doc.data()).toList();
          final objects = snaps.map((json) => fromJson(json)).toList();

          sink.add(objects);
        },
      );
}
0 Answers
Related