What are comments in dart and how many types of comments in dart?

Comments are a set of statements that are not executed by the compiler. The use of comments makes it easy for humans to understand the source code. Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.

Types of Dart Comments

In Dart, there are 3 types of comments.

  • Dart Single Line Comment
  • Dart Multi Line Comment
  • Dart Documentation Comment

Single-Line Comment In Dart

single-line comment extends up to the newline character and is specified with a double forward slash (//). A single-line comment can be used until a line break is reached.

Syntax

The syntax for a single-line comment is as follows.

// A single comment line

Example

The example below shows how you can use single-line comments in your code.

void main() {
// This is single-line comment.
print("Welcome to flutterfever.com.");
}

Multi-line comments

Multi-line comments can be used when you need to apply comments to multiple lines.

Syntax

The syntax for a multi-line comment is as follows.

/*
This is a 
multi-line  
comment 
*/

The compiler disregards everything between /* and */. However, a multi-line comment cannot be nested within other multi-line comments.

void main(){  
/*
This is a multi-line comment.
*/
    print("Welcome to flutterfever.com.");  
}  

Documentation comments

Documentation comments are a special form of comments that are mostly used to create documentation or a reference for a project or software package.

A documentation comment can be a single-line or multi-line comment that contains the /// or /* characters at the beginning. You can use /// on successive lines, which is the same as the multi-line comment. Except for lines written inside the curly brackets, the Dart compiler ignores these lines.

Syntax

The syntax for a documentation comment is as follows

///This  
///is   
///an example of  
/// a documentation comment 

Example

The example below shows how you can use documentation comments in your code.

void main(){  
/// This is documentation comment
    print("Welcome to flutterfevr.com.");  
}  
dart comments

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More