0
I have a problem with the conversion of String for double. As you can see in the code, my if for in the variable latas and difvalor because you’re making the mistake:
A value of type 'double' can’t be Assigned to a variable of type 'int'. Try Changing the type of the variable, or casting the right-hand type to 'int'.
How can I fix this conversion problem?
Follows the code
Future calculate() {
var totp1 =  double.parse(_precoCtrl.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12;
var totp2 = double.parse(_precoCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',))* 12;
var totmlp1 = int.parse(_unitCtrl.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12;
var totmlp2 = int.parse(_unitCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12;
var difvalor = 0;
var latas = 0;
var litros = 0;
var totlitros = 0;
var diflitros = 0;
var precounit1 = int.parse(_precoCtrl.text.replaceAll(new RegExp(r'[,.]'), '',));
var precounit2 = int.parse(_precoCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',));
var precoml1 = int.parse(_unitCtrl.text.replaceAll(new RegExp(r'[,.]'), '',));
var precoml2 = int.parse(_unitCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',));
//totp1 = double.parse(_precoCtrl.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12 as int;
//totp2 = double.parse(_precoCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',))* 12 as int;
//totmlp1 = double.parse(_unitCtrl.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12 as int;
//totmlp2 = double.parse(_unitCtrl1.text.replaceAll(new RegExp(r'[,.]'), '',)) * 12 as int;
setState(() {
  _completed = false;
  _busy = true;
});
return new Future.delayed(const Duration(seconds: 1), ()  {
  setState(
    () {
      if (totp1 > totp2) {
        difvalor = (totp1 - totp2);
        latas = difvalor / precoml2;
        litros = latas * double.parse(_unitCtrl.text) as int;
        totlitros = litros + totmlp1;
        diflitros = totlitros - totmlp1;
        if (diflitros > 0) {
        _resultText1 = 'Produto 2';
        _resultText =
            ('Comprando mais '+ latas.toStringAsFixed(0) +
                    ' unidades do Produto 2, sairá o mesmo preço que o Produto 1. Porém levando o Produto 2 você vai ganhar ' +
               diflitros.toStringAsFixed(0) +
                ' ml a mais!');
      } else if (diflitros < 0)
        _resultText = 'Pelo mesmo preço você leva ' + latas.toStringAsFixed(0)+
            ' unidades do Produto 1. Porém levando o Produto 2, você ganhará '+diflitros.toStringAsFixed(0)+' ml a mais!';
      else if (diflitros == 0) {
      _resultText1 = 'Produto 2';
        _resultText =
            'Levando '+ latas.toStringAsFixed(0)+' do Produto 2, fica o mesmo preço que levar a Produto 1 e não há nenhuma diferença de ML';
      }
      } else {
        difvalor = (totp2 - totp1);
        latas = (difvalor / double.parse(_precoCtrl.text)) as int;
        litros = latas * double.parse(_unitCtrl1.text) as int;
        totlitros = litros + totmlp2;
        diflitros = totlitros - totmlp1;
      }
        if (diflitros > 0) {
      _resultText1 = 'Produto 1';
      _resultText =
            ('Comprando mais '+ latas.toStringAsFixed(0) +
                    ' unidades do Produto 1, sairá o mesmo preço que a Produto 2. Porém levando o Produto 1 você vai ganhar ' +
                    diflitros.toStringAsFixed(0) +
                    ' ml a mais!');
                    _resultText1 = 'Produto 1';
          } else if (diflitros < 0)
            _resultText = 'Pelo mesmo preço você leva ' + latas.toStringAsFixed(0)+
                ' unidades do Produto 2. Porém levando o Produto 1, você ganhará '+diflitros.toStringAsFixed(0)+' ml a mais!';
          else if (diflitros == 0) {
            _resultText1 = 'Produto 1';
            _resultText =
                'Levando '+ latas.toStringAsFixed(0)+' unidades do Produto 1, fica o mesmo preço que levar o Produto 2 e não há nenhuma diferença de ML';
          }
          _busy = false;
          _completed = true;
        },
    );
    });
  }
						
Good guy! Thanks for your help.
– Matheus Lima
Dispose. If the answer has solved the problem I recommend to signal as accepted.
– Leonardo Paim