Collection.fromGraphJson constructor

Collection.fromGraphJson(
  1. Map<String, dynamic> json
)

Implementation

factory Collection.fromGraphJson(Map<String, dynamic> json) {
  Map<String, dynamic> nodeJson = json['node'] ?? const {};
  if (json.containsKey('nodes')) {
    nodeJson = json['nodes'][0] ?? const {};
  }

  var _products = Products.fromGraphJson(nodeJson['products'] ?? const {});
  final _realProducts = <Product>[];

  for (final _product in _products.productList) {
    final _realProductVariants = <ProductVariant>[];
    for (final _variant in _product.productVariants) {
      if (_variant.title.toLowerCase().contains('default')) {
        _realProductVariants
            .add(_variant.copyWith.call(title: _product.title));
      } else {
        _realProductVariants.add(_variant);
      }
    }
    _realProducts
        .add(_product.copyWith.call(productVariants: _realProductVariants));
  }

  _products = _products.copyWith.call(productList: _realProducts);

  return Collection(
    title: nodeJson['title'],
    description: nodeJson['description'],
    descriptionHtml: nodeJson['descriptionHtml'],
    handle: nodeJson['handle'],
    id: nodeJson['id'],
    updatedAt: nodeJson['updatedAt'],
    image: nodeJson['image'] != null
        ? ShopifyImage.fromJson(nodeJson['image'])
        : null,
    products: _products,
    cursor: json['cursor'],
  );
}