Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting an error the argument type 'Object?' can not b assigned to 'Object' in flutter

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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();
  }


}

>Solution :

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);
      }
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading