Writing Comments in Dart

Any line starting with a double forward slash will be considered a comment by the Dart compiler. This means that this part will not run or appear to the users of this application because it will remain internal. Comments are used to write notes about different parts of the Dart program.

Example:
In the previous code, you may add the following line as a comment:

// The following codes to input data to Dart program


The code follows:

import 'dart:io';

main() {

// The following codes to input data to Dart program

  print('Please enter the first number:');

  int num1=int.parse(stdin.readLineSync());


  print('Please enter the second number:');

  int num2=int.parse(stdin.readLineSync());

  var sum=num1+num2;

  print('The sum result = $sum');

  }


When you run this program, the part which starts with a double forward slash will not appear to users.
Also, if you want to stop any part of the program temporarily, you can do that by adding a double forward slash at the beginning of this line of code as illustrated in the grey highlighted part of the following code:

import 'dart:io';

main() {
// The following codes to input data to Dart program
// print('Please enter the first number:');

  int num1=int.parse(stdin.readLineSync());

  print('Please enter the second number:');

  int num2=int.parse(stdin.readLineSync());

  var sum=num1+num2;

  print('The sum result = $sum');

  }


Besides, you may configure a block of comments which include more than one line if it starts with /* and ends with */. These (/* & */) are used to add multiple lines of comments in the code without adding // at the beginning of each line as illustrated in the following code:

import 'dart:io';

main() {


 /* print('Please enter the first number:');

  int num1=int.parse(stdin.readLineSync());


  print('Please enter the second number:');

  int num2=int.parse(stdin.readLineSync());

  var sum=num1+num2;

  print('The sum result = $sum');

  */

  }


If you run this program, you will get the following:

* This topic is a part of lesson 2 of Flutter Application Development course. For more information about this course, click here.