Home > front end >  error: Conditions must have a static type of 'bool'
error: Conditions must have a static type of 'bool'

Time:01-18

I tried to make a calculator in flutter for the first time, but an error occur at the bool type that i don't understand. (on the "if" opertaion)

class _CalculatorAppState extends State<CalculatorApp> {
  int? firstNum;
  int? secondnum;
  String? textToDisplay;
  String? history = '';
  String? res = '';
  String? operation;

  void btnOnClick(String btnVal){
    print(btnVal);
    if( btnVal == 'C'){
      textToDisplay = '';
      firstNum = 0;
      secondnum = 0;
      res= '';
    } else if (btnVal == 'C'){
      textToDisplay = '';
      firstNum = 0;
      secondnum = 0;
      res= '';
      history = '';
    } else if ( btnVal == ' '||
        btnVal == '-'||
        btnVal == '/'||
        btnVal == 'X') {
      firstNum = int.parse(textToDisplay!);
      res = '';
      operation = btnVal;
    } else if (btnVal == '='){
      secondnum = int.parse(textToDisplay!);
      if(operation = ' ') {
        res = (firstNum!   secondnum!).toString();
        history = firstNum.toString()   operation.toString()   secondnum.toString();
      }
      if(operation = '-') {
        res = (firstNum! - secondnum!).toString();
        history = firstNum.toString()   operation.toString()   secondnum.toString();
      }
      if(operation = 'X') {
        res = (firstNum! * secondnum!).toString();
        history = firstNum.toString()   operation.toString()   secondnum.toString();
      }
      if(operation = '/') {
        res = (firstNum! / secondnum!).toString();
        history = firstNum.toString()   operation.toString()   secondnum.toString();
      } else {
        res = int.parse(textToDisplay!   btnVal).toString();
      }

      setState(() {
        textToDisplay = res;
      });
    }
  }

error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:46)

error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:50)

error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:54)

error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:58)

CodePudding user response:

You're currently typing operation = 'x' which isnt valid syntax. You need a double '=', so:

if (operation == 'x') {
  // do stuff
}

CodePudding user response:

The equality operator is == not =.

At those lines listed you used = instead of ==.

And declare the variables as follows:

var firstNum = 0;
var secondnum = 0;
var textToDisplay = '';
var history = '';
var res = '';
var operation = '';
  •  Tags:  
  • Related