Search code examples
flutterdartcontacts

The argument type 'Item' can't be assigned to the parameter type 'List<dynamic>'


I am trying to iterate over a phone list to display mobile numbers for the contact using package contacts_service.

The class UserContactItem holds the info of the contact and when I pass it`s elements to userContact it gives me this error:

Compiler message:
lib/home.dart:117:28: Error: The argument type 'Item' can't be assigned to the parameter type 'List<dynamic>'.
 - 'Item' is from 'package:contacts_service/contacts_service.dart' ('../../../../../../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/contacts_service-0.3.10/lib/contacts_service.dart').
 - 'List' is from 'dart:core'.
          number: mobilenum[0],
                           ^

here is class UserContactItem:

class UserContactItem{


final String contactName;
  final List number;

  UserContactItem({this.contactName, this.number});
}

here is the iteration over phone elements:

final Iterable<Contact> contacts = await ContactsService.getContacts();

var iter = 0;
contacts.forEach((contact) async{
  var mobilenum = contact.phones.toList();

  if(mobilenum.length != 0){
    var userContact = UserContactItem(
      contactName: contact.displayName,
      number: mobilenum[0],
    );
  }
});

Solution

  • Initially, in the class UserContactItem, you have defined that the number will be of type List<dynamic>.

    class UserContactItem{
    final String contactName;
      final List number; //here
      UserContactItem({this.contactName, this.number});
    }
    

    And later, you are assigning mobilenum[0] to it which is of type Item. Which can't be assigned.

    You should change the type of number in UserContactItem to Item.

    class UserContactItem{
    final String contactName;
      final Item number; //like this
      UserContactItem({this.contactName, this.number});
    }