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
You need to specify the list type
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:
Make the attributes inside CardModel to be nullable ( String? ).
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.
You’ve to explicitly specify types:
Read more about Sound Typing in Dart Type System documentation.