I am creating a demo of hive getx and here I want to initilize transactions from box's values
but I am getting an error The Argument Type 'TransactionModel?' can't be assign to the parameter type 'TransactionModel'
here is my coding
class TransactionController extends GetxController
{
List<TransactionModel> _transactions=[];
Box<TransactionModel> transactionbox=Hive.box('transactionsdb');
List<TransactionModel> get transactions{
return _transactions;
}
TransactionController(){
for(int x=0;x<transactionbox.values.length;x )
{
_transactions.add(transactionbox.getAt(x));
//this statement showing error line
}
}
int get countlength{
return _transactions.length;
}
void addTransaction(TransactionModel transaction)
{
_transactions.add(transaction);
update();
}
void deleteTransaction(String id)
{
_transactions.removeWhere((element) => element.id==id);
update();
}
// void swapTransaction(TransactionModel transaction)
// {
// transaction.isExpense=!transaction.isExpense;
// update();
// }
void swapTransaction(TransactionModel transaction)
{
int index=_transactions.indexOf(transaction);
_transactions[index].isExpense=!_transactions[index].isExpense;
update();
}
}
CodePudding user response:
You must check if the value of transactionbox.getAt(x)
is null
or not befor add it to the _transactions
list.
TransactionController(){
for(int x=0;x<transactionbox.values.length;x ) {
var valueToCheck = transactionbox.getAt(x);
if (valueToCheck != null) {
_transactions.add(valueToCheck);
}
}
}