skip to Main Content

I have a problem with dart programming language.

ERROR: The argument type ‘Object?’ can’t be assigned to the parameter type ‘String’.

I don’t understand what is wrong. please help me.

This is the CardModel in my flutter project.

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": "Berfin Gürz",
    "cardNumber": "**** **** **** 1727",
    "cardExpired": "17-01-2022",
    "cardType": "assets/images/mastercard_logo.png",
    "cardBackground": 0xFF1E1E99,
    "cardElementTop": "assets/svg/ellipse_top_pink.svg",
    "cardElementBottom": "assets/svg/ellipse_bottom_pink.svg"
  },
  {
    "user": "Berfin Gürz",
    "cardNumber": "**** **** **** 8213",
    "cardExpired": "03-01-2024",
    "cardType": "assets/images/mastercard_logo.png",
    "cardBackground": 0xFFFF70A3,
    "cardElementTop": "assets/svg/ellipse_top_blue.svg",
    "cardElementBottom": "assets/svg/ellipse_bottom_blue.svg"
  }
];

3

Answers


  1. You need to specify the list type

    List<Map<String,dynamic>> cardData = [
      {
        "user": "Berfin Gürz",
        "cardNumber": "**** **** **** 1727",
        "cardExpired": "17-01-2022",
        "cardType": "assets/images/mastercard_logo.png",
        "cardBackground": 0xFF1E1E99,
        "cardElementTop": "assets/svg/ellipse_top_pink.svg",
        "cardElementBottom": "assets/svg/ellipse_bottom_pink.svg"
      },
      {
        "user": "Berfin Gürz",
        "cardNumber": "**** **** **** 8213",
        "cardExpired": "03-01-2024",
        "cardType": "assets/images/mastercard_logo.png",
        "cardBackground": 0xFFFF70A3,
        "cardElementTop": "assets/svg/ellipse_top_blue.svg",
        "cardElementBottom": "assets/svg/ellipse_bottom_blue.svg"
      }
    ];
    
    Login or Signup to reply.
  2. I think you have to take into account that the object you retrieve from your map, could be null. On the examples that you give there it isn’t the case, but since you are handling maps it could happen that one of the entries does not exists.

    To make your code run you can do two things:

    1. Make the attributes inside CardModel to be nullable ( String? ).

    2. On the map, you could ensure that what you retrieve from the map is a non-null string and then pass it to the instantiation of the model. It could be with null checks before making the isntance and saving the values of the map into variables.

    Login or Signup to reply.
  3. You’ve to explicitly specify types:

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

    Read more about Sound Typing in Dart Type System documentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search