Home > Blockchain >  How to do math operations in flutter?
How to do math operations in flutter?

Time:06-12

It seems easy, but I get always 0 when i run the math operations in my flutter app. I did the same in Java with success, but i'm sure it is slightly different in flutter. Basically, after run the operation, the button will execute the add method and put the result on a textfield.

Something i also don't get it, if my variables are defined as double, why then i need to say again "ris1 = (q1 ~/ sum) as double;" ?

I am trying to run this:( i just wrote the essential code, because it is too big)

class _CalcolaWState extends State<CalcolaW> {


TextEditingController _textEditingController = TextEditingController();

double q1 = 0, q2 = 0, f1 = 0, f2=0;
double sum = 0, ris1=0, ris2 = 0, ris3 = 0, ris4= 0, ris5=0;

add() {
setState((){
    f1 = double.parse(_textEditingController.text);
    q1 = double.parse(_textEditingController2.text);
    f2 = double.parse(_textEditingController3.text);
    q2 = double.parse(_textEditingController4.text);

    sum = (q1   q2);
      
    ris1 = (q1 ~/ sum) as double;
      
    ris2 = (q2 ~/ sum) as double;

    ris3 = f1 * ris1;
    ris4 = f2 * ris2;
    ris5 = ris3   ris4;

    });
    }
    CupertinoTextField(
        padding: EdgeInsets.all(12),
        keyboardType: TextInputType.number,
        controller: _textEditingController,
        placeholder: 'Enter 1st Flour Strenght ' ,
        placeholderStyle: TextStyle(color: cursorColor),
        ),
    ),
    ElevatedButton(
        child: Text("Calculate"),
        style: ElevatedButton.styleFrom(
        primary: Colors.green,
        ),
        onPressed: (){
        add();
        _controller.text = '$ris5';
        print(ris5);
        }

CodePudding user response:

~/ operator returns integer, use / for double

~/ Divide, returning an integer result

See Arithmetic operators.

  • Related