Comments, & const Vs final Keywords in Dart 2025
Understanding Comments & Their Types, const and final keywords, and their uses in Dart for 2025

The set of statements which are ignored by Dart Compiler, are called Comments. They are used to provide more information about the written code, or they provide brief idea of what is happening in the code. Comments play important role in enhancing code readability, and help other developers to understand it when required.
In Dart, there are three types of Comments.
Single-line Comments
Single-line comments as name suggests are based on one line. They give short information about the following line/s of code. They start and end with //. Everything between them is ignored by Dart. In modern IDEs, you should just type // in the beginning, compiler automatically ignore that particular line.
void main(){
int a = 10;
int b = 20;
// this adds a and b
int sum = a + b;
print(“sum is $sum”);
}
Multi-line Comments
Multi-line comments are used when the information about code is needed to be written in more than one line. They start with /*, and end */. All the lines between them are ignored by Dart.
void main(){
int a = 10;
int b = 20;
/*
A is integer data type that contains 10.
B is also integer data type that contains 20.
Sum is integer data types that is written below.
Sum adds both a and b.
All of these lines are ignored by compiler.
*/
int sum = a + b;
print(“sum is $sum”);
}
Documentation Comments
Documentation comments start with /// or /**. They can single or multi-line comments. They are used when writing documentation of a project or such scale information about the code.
void main(){
int a = 10;
int b = 20;
///A is integer data type that contains 10.
///B is also integer data type that contains 20.
///Sum is integer data types that is written below.
///Sum adds both a and b.
///All of these lines are ignored by compiler.
int sum = a + b;
print(“sum is $sum”);
}
final & const Keywords in Dart
Previously we have understood with example the Dart Variables. In this article, I have explained what are final, and const keywords in Dart, the syntax, when to use them, and the difference between them.

We know that we can assign the values to variables, and we can also change them.
var name = “Ali”; // variable name is Ali
print(“name”); // prints value of name that is Ali.
// changing the value of variable “name”.
name = “Ahmed”; // change the value of name from Ali to Ahmed
print(“name”); // value of name will be printed that will be Ahmed since we have changed it from Ali above.
I hope you have understood it till now. But what if you don’t want to change the value of a variable. There are many cases in programming when you don’t need to change the value, and value that you assign to a variable at first time, that remains unchanged throughout the program. In such cases const, and final keywords come into the picture.
Final & Const
In Dart, both final, and const keywords are immutable. Immutable means their values can’t be changed. To make variables const or final, just the keyword before the variable name if it isn’t type casted or before the its type if its type is mentioned. See Type Casting here.
Using final
final fullName = “Ali Ahmed”; // without type casting
final String FirstName = “Ali”; // with type casting
We know that the value of const and final can’t be changed.
fullName = “Khan”; // this is wrong because we have already assigned value to it.
Using const
const pi = 3.14; // without type annotation or type casting
const double percentage = 87.6; // with type casting.
Since the value of const can’t be change so,
percentage = 89.5; // this is wrong.
Final vs Const: The Difference
We know that both final and const are immutable, and their values can’t be changed. But it is important to understand the difference between them in order to write a professional code.
Final
Final keyword is set at only once, and it is initialized when it is accessed. It means when you create a final variable, and assign a value to it, and don’t use it in your program then it doesn’t occupy the memory in your computer.
Const
Const keyword is compile time const. It means that it is assigned a memory location when program is compiled even if you don’t use it. If you created a const variable, the memory location will be assigned to it during compile time whether you use it or not.
That’s all for this article. I hope you have understood the concepts, and their implementation well. Thank you for reading this article.



Comments
There are no comments for this story
Be the first to respond and start the conversation.