Home > Net >  Creating list of user defined objects throws The argument of type string cant be assigned to paramet
Creating list of user defined objects throws The argument of type string cant be assigned to paramet

Time:04-25

I am trying to create a list of user defined objects. As in the snippet below it will be a list of Phone objects. When I am trying to add a String value to the list I get the error - The argument of type string cant be assigned to parameter type Phones

void main() {
  List<Phones>? phones;
  phones.add('Test String'); -- ***Error is on this line***
}

class Contact {
  String? name;
  List<Phones>? phone;  
}

class Phones {
  String? phone;
}

CodePudding user response:

List<Phones>? phones;

Is as described a list of phones(object), Not of string

Make a constructor for your class to accept a String phone value

class Phones {
  String? phone;
  Phones(this.phone);
}

Then add the phone string this way

phones.add(Phones("Test String"));

You later can access the phone string like this

phones[index].phone
  • Related