skip to Main Content

Tell me what is the problem?

Flutter version 3.7.1 on channel stable
Dart version 2.19.1

In
"item": List<Map<String, dynamic>>.from(item.map((x) => x.toJson()))

error(underline red color) .map – Missing type arguments for generic method ‘map’.,
what is wrong in this code?

BuyCard buyCardFromJson(String str) => BuyCard.fromJson(json.decode(str));

String buyCardToJson(BuyCard data) => json.encode(data.toJson());

class BuyCard {

  BuyCard({
    this.cashierName,
    this.date,
    this.commit,
    this.phone,
    this.type,
    this.charge,
    this.items,
  });

  factory BuyCard.fromJson(Map<String, dynamic> json) => BuyCard(
    cashierName: json["cashierName"],
    date: json["date"],
    commit: json["commit"],
    phone: json["phone"],
    type: json["type"],
    charge: json["charge"],
    items: Items.fromJson(json["items"]),
  );
  final String cashierName;
  final String date;
  final int commit;
  final String phone;
  final int type;
  final int charge;
  final Items items;

  Map<String, dynamic> toJson() => <String, dynamic>{
    "cashierName": cashierName,
    "date": date,
    "commit": commit,
    "phone": phone,
    "type": type,
    "charge": charge,
    "items": items.toJson(),
  };
}


///-----
class Items {

  Items({
    this.item,
  });

  factory Items.fromJson(Map<String, dynamic> json) => Items(
    item: List<Item>.from(json["item"].map((dynamic x) => Item.fromJson(x))),
  );

  final List<Item> item;
  Map<String, dynamic> toJson() => <String, dynamic>{
    "item": List<Map<String, dynamic>>.from(item.map((x) => x.toJson())),
  };
}


///------
class Item {

  Item({
    this.pos,
    this.sku,
    this.qty,
    this.price,
  });

  factory Item.fromJson(Map<String, dynamic> json) => Item(
    pos: json["pos"],
    sku: json["SKU"],
    qty: json["qty"],
    price: json["price"],
  );
  final int pos;
  final String sku;
  final int qty;
  final int price;

  Map<String, dynamic> toJson() => <String, dynamic>{
    "pos": pos,
    "SKU": sku,
    "qty": qty,
    "price": price,
  };
}

I tried to generate from the site but the same problem

2

Answers


  1. Chosen as BEST ANSWER

    Solved

    Added an argument map<dynamic>

      Map<String, dynamic> toJson() => <String, dynamic>{
        "item": List<Map<String, dynamic>>.from(item.map<dynamic>((x) => x.toJson())),
      };
    

  2. That’s because the Dart compiler can not infer the type of the T in map<T> function from the context.
    You are passing your item.map(... to List<Map<String, dynamic>>.from( which expects simply an Iterable and not type is defined for the Iterable.

    define the output type of the Iterable of your map statement. here in your case is Map<String, dynamic> or can do dynamic

    Map<String, dynamic> toJson() => <String, dynamic>{
        "item": List<Map<String, dynamic>>.from(item.map<Map<String, dynamic>((x) => x.toJson())),
      };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search