fromMap static method

dynamic fromMap(
  1. Map<String, dynamic> map, {
  2. required bool isProto,
})

Implementation

static fromMap(Map<String, dynamic> map, {required bool isProto}) {
  if (isProto == false) {
    final kind = (map['article']['kind'] ?? '') as String;
    if (kind == ArticleKind.uncountable.toString()) {
      return _fromMapUncountable(map);
    } else if (kind == ArticleKind.basket.toString() ||
        map['article']['proxies'] != null) {
      return _fromMapBasket(map);
    } else {
      // (kind == ArticleKind.retail.toString()) is always true if no kind so last
      return _fromMapRetail(map);
    }
  } else {
    /// if is Proto we could get items like this :
    /// {articleRetail: {}, articleBasket: {}, articleUncountable: {calibreId: -5, id: 1, designation: vendredi 31 total vente, price: 137500, cost: 0}, quantity: 1}
    /// so we build another map without the empty useless types, while conserving the quantity
    /// and we handle each specific case
    final cleanMap = <String, dynamic>{};
      print('map.entries ${map.entries}');

    for (final entry in map.entries) {
      if (entry.key == 'quantity' && entry.value is num) {
        cleanMap[entry.key] = entry.value;
      } else {
        if (entry.value is Map && (entry.value as Map).isNotEmpty) {
          cleanMap[entry.key] = entry.value;
        }
      }
    }
    if (cleanMap.keys.contains('articleUncountable')) {
      return _fromMapUncountableProto(cleanMap);
    } else if (cleanMap.keys.contains('articleBasket')) {
      return _fromMapBasketProto(cleanMap);
    } else if (cleanMap.keys.contains('articleRetail')) {
      print('cleanMap $cleanMap');
      return _fromMapRetailProto(cleanMap);
    } else {
      throw 'unknown article in item $cleanMap';
    }
  }
}