我正在嘗試創建用戶定義物件的串列。在下面的代碼片段中,它將是一個 Phone 物件串列。當我嘗試將字串值添加到串列中時,出現錯誤 - 字串型別的引數不能分配給引數型別電話
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;
}
uj5u.com熱心網友回復:
List<Phones>? phones;
如所描述的電話串列(物件),不是字串
為您的類創建一個建構式以接受字串電話值
class Phones {
String? phone;
Phones(this.phone);
}
然后以這種方式添加電話字串
phones.add(Phones("Test String"));
您稍后可以像這樣訪問電話字串
phones[index].phone
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464476.html
上一篇:Flutter錯誤:StreamProvider在添加空安全后需要initialData引數。來自firebase的StreamProvider<UserModel>
