0

I am following a video on youtube of Flutter UI, In which the tutor is mapping JSON-like data to a more specific custom-made object class but doing the same is throwing errors to me. Might be because of the new Dart version?. Can someone clear the mapping concept to me as I have no idea and also how to fix this error?

Errors:

Facing these errors

  class CardModel {
  String user;
  String cardNumber;
  String cardExpired;
  String cardType;
  int cardBackground;
  String cardElementTop;
  String cardElementBottom;

  CardModel(this.user, this.cardNumber, this.cardExpired, this.cardType,
      this.cardBackground, this.cardElementTop, this.cardElementBottom);
}

List<CardModel> cards = cardData
    .map(
      (item) => CardModel(
        item['user'],
        item['cardNumber'],
        item['cardExpired'],
        item['cardType'],
        item['cardBackground'].,
        item['cardElementTop'],
        item['cardElementBottom'],
      ),
    )
    .toList();

var cardData = [
  {
    "user": "Amanda Alex",
    "cardNumber": "**** **** **** 1425",
    "cardExpired": "03-01-2023",
    "cardType": "assets/images/mastercard_logo.png",
    "cardBackground": 0xFF1E1E99,
    "cardElementTop": "assets/svg/ellipse_top_pink.svg",
    "cardElementBottom": "assets/svg/ellipse_bottom_pink.svg"
  },
  {
    "user": "Amanda Alex",
    "cardNumber": "**** **** **** 8287",
    "cardExpired": "03-01-2025",
    "cardType": "assets/images/mastercard_logo.png",
    "cardBackground": 0xFFFF70A3,
    "cardElementTop": "assets/svg/ellipse_top_blue.svg",
    "cardElementBottom": "assets/svg/ellipse_bottom_blue.svg"
  }
];

Second File

class OperationModel {
  String name;
  String selectedIcon;
  String unselectedIcon;

  OperationModel(this.name, this.selectedIcon, this.unselectedIcon);
}

List<OperationModel> datas = operationsData
    .map((item) => OperationModel(
        item['name'], item['selectedIcon'], item['unselectedIcon']))
    .toList();

List<Map<String, dynamic>> operationsData = [
  {
    "name": "Money\nTransfer",
    "selectedIcon": "assets/svg/money_transfer_white.svg",
    "unselectedIcon": "assets/svg/money_transfer_blue.svg"
  },
  {
    "name": "Bank\nWithdraw",
    "selectedIcon": "assets/svg/bank_withdraw_white.svg",
    "unselectedIcon": "assets/svg/bank_withdraw_blue.svg"
  },
  {
    "name": "Insight\nTracking",
    "selectedIcon": "assets/svg/insight_tracking_white.svg",
    "unselectedIcon": "assets/svg/insight_tracking_blue.svg"
  },
];

enter image description here

Here in this second file when I changed map as <String, String> this error occurs but changing <String, dynamic> works fine. But why mentioning string is not working as these all are Strings in operationModel class

1 Answer 1

1

You need to let Dart know what types are in cardData like this:

List<Map<String, dynamic>> cardData = [
  //...
];

instead of var cardData...

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks that's working. A little bit of confusion in another file when I explicitly mentioned that the map is <String, String> it's giving the same error but changing <String, dynamic> is working why? . In that file all of the class members are string only.
I'm not sure. You would have to show the code and error.
added the second file can you explain now
when you use the [] method, there is no guarantee that it won't return null, so it's of type String?, which can't be assigned to type String. However, the dynamic type can be assigned to String variables. You also could have fixed this with a null check, for instance: item['name']!, instead of just item['name']
it's related to null-safety, which you can read about here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.