Gheh
Gheh

Reputation: 305

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

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

Upvotes: 6

Views: 29313

Answers (4)

DevMukh
DevMukh

Reputation: 29

 **int amt = 0; 
 amt = int.tryParse(widget.price)!;
setState(() {
      _increment();
      sum = sum + amt;
    });
in this code widget.price comes from previcious widgets and price is actually  stored in a Firebase as a string so then i convert into int first and assign a new variable i works for me**

Upvotes: 0

neelkothari6191
neelkothari6191

Reputation: 61

Try changing the datatype to 'dynamic' instead of 'int'

Upvotes: 4

Sajib saha
Sajib saha

Reputation: 371

 setState(() {
    sliderlength = _carouselImages!.length;
  });

This will fix your problem .

Upvotes: 3

Gheh
Gheh

Reputation: 305

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

Upvotes: 12

Related Questions