Home > Enterprise >  How to fix argument type 'int?' can't be assigned to the parameter type 'num
How to fix argument type 'int?' can't be assigned to the parameter type 'num

Time:11-29

I am trying to do a computation. But my value is from a int? and it not letting me to do it. the error is The argument type 'int?' can't be assigned to the parameter type 'num'. I don't understand.

Here is my code:

  String goal= "1000";
  String workout = "0";
  String remaining = "";
  int? _total;

  @override
  void initState() {
    super.initState();
    dbHelper = DbHelper();
    _calcTotal();
  }

  void _calcTotal() async{
    var total = (await dbHelper.calcTotal())[0]['total'];
    print(total);
    setState(() => _total = total);
  } 



int resulttext = int.parse(goal) - _total   int.parse(workout);
remaining = resulttext.toString();

CodePudding user response:

I solved my problem by this. I just add this line:

var total = _total?.toInt() ?? 0;

before this:

int resulttext = int.parse(goal) - (total   int.parse(workout));

CodePudding user response:

Try this in _calcTotal() function:

  void _calcTotal() async{
    var total = (await dbHelper.calcTotal())[0]['total'];
    print(total);
    setState(() => _total = int.parse(total.toString()));
  } 
  • Related