I have created a model class for account of person, where i want to assing transactions to empty array..
i have assigned like this.transactions=[],
here is my modelclass
class AccountModel {
String id;
String name;
DateTime createdOn;
List<TransactionModel> transactions;
bool isLocked;
double payTotal;
double receiveTotal;
double balance;
TransactionModel? lastEntry;
AccountModel(
{
required this.id, required this.name,
this.transactions= [],// this is showing an error
this.isLocked=false,
this.balance=0,
required this.createdOn,
this.lastEntry,
this.payTotal=0,
this.receiveTotal=0,
});
}
2
Answers
Here is
List<TransactionModel> transactions;
. Notdynamic
type. You assigned withdynamic
empty list. Can initialize withTransactionModel
object.The error says exactly what’s wrong.
To solve make it a constant by writing const in front of it:
EDIT:
As you have found out, a const list is unmodifiable so it’s not really desired to have. I see two options to solve this. The first is to not make it part of the constructor at all and always initialize it with
[]
so likeIf you really need it to be part of the constructor maybe this work around works for you: