0

The below snippet of code gives me error in dartpad. But the same code works fine in the course which I am doing online. I am using dart version 2.8.4.

void main() {
  
 int var = operation(5,5,add);  --> Error
  
  //print(operation(5,5,add));
}

// class calculator {
  
//   calculator({this.operand});
  
// }
  

int add(int n1, int n2) {
  return n1+n2;
}

int multiply(int n1, int n2) {
  return n1*n2;
}

int operation(int n1, int n2, Function operand){
  return operand(n1, n2);
}

Error:

Error compiling to JavaScript:
main.dart:3:2:
Error: Expected ';' after this.
 int var = operation(5,5,add);
 ^^^
main.dart:3:10:
Error: Expected an identifier, but got '='.
 int var = operation(5,5,add);
         ^
Error: Compilation failed.

1 Answer 1

3

int var = operation(5,5,add); don't use var as a variable name, it a keyword used in the dartlang, just change the name to anything else and you wont get any error:

int operationResult = operation(5,5,add);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.