Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading