i want to make this code excuted in the least number of code lines in dart

Advertisements
int a = 10;
int b = 20;


int Istru(bool c, int num1, int num2){
  return c?num1:num2;
}

print(Istru(true, 10, 220));

it makes that error when i run it:

Error compiling to JavaScript:
lib/main.dart:9:19:
Error: Expected an identifier, but got '10'.
print(Istru(True, 10, 220));
                  ^^
lib/main.dart:9:23:
Error: Expected an identifier, but got '220'.
print(Istru(True, 10, 220));
                      ^^^
lib/main.dart:9:28:
Error: Expected a function body or '=>'.
print(Istru(True, 10, 220));
                           ^
Error: No 'main' method found.
Error: Compilation failed.

>Solution :

Welcome to Dart. In Dart, you cannot have normal program execution directly in global scope like you can in e.g. JavaScript. That means in your case, you can’t just have a print statement being outside any method/function.

All program execution in Dart starts from a function called main() so we need to have such method to even run your program.

I suggest writing something like this:

int isTrue(bool c, int num1, int num2) {
  return c ? num1 : num2;
}

void main() {
  int a = 10;
  int b = 20;

  print(isTrue(true, 10, 220));
}

I also fixed some spelling mistakes and replaced True with true.

Leave a ReplyCancel reply