Semicolon in Dart

In Dart, a semicolon (;) is used to terminate a statement. It signals the end of the statement and is essential for proper code execution. Without a semicolon, the code will produce a syntax error.

📅 Mon Jan 13 2025⏱ 5 min
image

Why is the Semicolon Important?

1) It indicates the end of a statement.
2) It is a mandatory part of the syntax in Dart.
3) Without it, Dart will throw a syntax error.

Example

void printMessage() {
  print("Welcome to Dart!");  // Semicolon terminates the statement
}

In this example :

1) The function printMessage() contains a statement where the print() function is called.
2) The semicolon ends the statement inside the function body.

Another example for better understanding

Imagine you are building an app where you store and display information about a blog. You will need to store values like the blog name, author, and number of posts. Here’s how you would write it:

void main() {
  var blogName = "Flutter";  // Name of the blog
  var author = "Jony";  // Author of the blog
  var numberOfPosts = 20;  // Number of posts published
  
  // Printing the details
  print("Blog: $blogName");
  print("Author: $author");
  print("Total Posts: $numberOfPosts");
}

In this example :

1) We are creating three variables: blogName, author, and numberOfPosts.
2) Each of these statements ends with a semicolon, which tells Dart that the statement has ended.

Related Posts

What is Flutter...??

Flutter is an open-source UI software development kit for building natively compiled applications for mobile, web, and desktop from a single codebase. It works on the Dart programming language and is famous for developing fast, expressive and flexible UIs and high performance output.

2 min

What is Dart in Flutter..??

Dart is the programming language which Flutter adopted to build a cross-platform application. Google has developed this language and optimized in UI, and it is suitable for the fast development of visually rich applications on Flutter.

2 min

What is main in Flutter...??

Every programming language has an entry point from which the main execution of the code begins. Dart has this point with the main() function-that is where the program first looks to start executing code.

2 min

What is print()..??

The print statement is used in Dart to display output to the console. It is very helpful for debugging so you can see the value of variables or whether particular parts of your code are running as you expect them to.

2 min

What are the comments in Dart?

Comments are used to explain and add notes in code. The compiler ignores the commented code during the time of execution of the program.

2 min

Identifiers in Dart

In Dart, identifiers are used to name variables, functions, classes, methods, parameters, and other components in your code. These names can include letters, digits, underscores (_), and dollar signs ($), but they must adhere to specific rules for valid naming

2 min