我正在尝试遍历电话列表以显示使用包contacts_service的联系人的手机号码。
类UserContactItem包含联系人的信息,当我将其元素传递给userContact时,出现此错误:
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],
^
这里是类UserContactItem:
class UserContactItem{
final String contactName;
final List number;
UserContactItem({this.contactName, this.number});
}
这里是电话元素的迭代:
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],
);
}
});
最初,在类UserContactItem中,您已定义该号码的类型为<< [List。
class UserContactItem{
final String contactName;
final List number; //here, defined as List<dynamic> by the compiler
UserContactItem({this.contactName, this.number});
}
然后,您要为其分配item类型的mobilenum [0]。无法分配。您应将UserContactItem中的数字类型更改为Item。
class UserContactItem{ final String contactName; final Item number; //like this UserContactItem({this.contactName, this.number}); }