Basic Dart Syntax, main() Function, Statements, and Comments
Dart is the programming language used with Flutter to build cross-platform applications.
JustAcademy's Flutter training introduces Dart as part of the Flutter learning path and covers
Dart programming fundamentals such as variables, data types, operators, control statements,
functions, and object-oriented programming. :contentReference[oaicite:0]{index=0}
Explore the complete course:
JustAcademy Flutter Training
Register for a course demo:
Register for Flutter Course Demo
1. Introduction to Dart Syntax
Syntax means the set of rules that define how code should be written in a programming
language. Dart syntax determines how variables, functions, statements, expressions,
classes, comments, and other programming elements are written.
A simple Dart program looks like this:
void main() {
print("Hello, Dart!");
}
Although this example is small, it demonstrates several important concepts:
void represents the return type of the function.
main() is the entry-point function.
{ } defines the function body.
print() displays information.
; terminates the statement.
"Hello, Dart!" is a string value.
2. Basic Structure of a Dart Program
A basic Dart program can contain imports, variables, functions, classes, and a
main() function.
// Import statements
import 'dart:math';
// Variables or constants
const String appName = "Dart Application";
// Functions
void greetUser() {
print("Welcome to Dart");
}
// Main function
void main() {
greetUser();
}
Main Components
Component |
Purpose |
|---|
Import |
Provides access to libraries and other code. |
Variable |
Stores information or data. |
Function |
Contains reusable logic. |
main() |
Starting point of a standard Dart application. |
Class |
Defines the structure and behavior of objects. |
Statement |
Represents an instruction executed by the program. |
Comment |
Provides information for developers and is ignored during execution. |
3. The main() Function
The main() function is the standard entry point of a Dart program.
When a normal Dart application starts, execution begins from this function.
void main() {
print("Application started");
}
Output:
Application started
Understanding main()
void main() {
// Program instructions
}
void indicates that the function does not return a value.
main is the function name.
() contains function parameters, if any.
{ } contains the function body.
4. Why is main() Important?
The main() function provides a clear starting point for program execution.
It is where the application can initialize data, call functions, create objects, and
begin other application logic.
void showMessage() {
print("Learning Dart");
}
void main() {
print("Program started");
showMessage();
print("Program finished");
}
Output:
Program started
Learning Dart
Program finished
5. main() Function with Parameters
Dart also allows the main() function to receive command-line arguments.
void main(List<String> arguments) {
print(arguments);
}
Here, arguments is a list of strings that can contain values passed to the
application through the command line.
6. Statements in Dart
A statement is an instruction that tells the program to perform an operation.
Most Dart statements end with a semicolon (;).
void main() {
int age = 25;
print(age);
}
In this example, the following are statements:
int age = 25;
print(age);
7. Variable Declaration as a Statement
Creating and assigning a variable is a common type of statement.
String name = "Rahul";
int age = 25;
double salary = 45000.50;
bool isStudent = false;
Each declaration is terminated with a semicolon.
8. Expression Statements
Expressions can also be used as statements when they perform an operation.
int a = 10;
int b = 20;
a + b;
print(a + b);
In practical programs, expressions are usually used as part of assignments,
function calls, conditions, or other operations.
9. Assignment Statements
Assignment statements assign or update values.
int age = 20;
age = 21;
print(age);
Output:
21
10. Function Call Statements
Calling a function is another common statement.
void greet() {
print("Hello!");
}
void main() {
greet();
}
The statement greet(); calls the greet() function.
11. Conditional Statements
Conditional statements allow the program to make decisions.
if Statement
void main() {
int age = 20;
if (age >= 18) {
print("You are an adult");
}
}
if-else Statement
void main() {
int age = 16;
if (age >= 18) {
print("Eligible");
} else {
print("Not eligible");
}
}
12. Curly Braces in Dart
Curly braces { } are used to define blocks of code.
They are commonly used with functions, conditions, loops, and classes.
void main() {
int number = 10;
if (number > 5) {
print("Number is greater than 5");
}
}
The code inside the braces belongs to the corresponding block.
13. Semicolons in Dart
Dart generally uses semicolons to indicate the end of statements.
int number = 10;
String name = "Amit";
print(name);
However, the closing brace of a normal function or control-flow block does not
normally require a semicolon.
void greet() {
print("Hello");
}
14. Comments in Dart
Comments are notes written inside source code for developers. They help explain
what code does, document important information, or temporarily describe logic.
Comments are not treated as executable program instructions.
Dart supports three commonly used comment styles:
- Single-line comments
- Multi-line comments
- Documentation comments
15. Single-Line Comments
A single-line comment starts with two forward slashes:
//.
// This is a comment
void main() {
print("Hello Dart");
}
Everything after // on that line is treated as a comment.
Example
void main() {
// Store the user's age
int age = 25;
// Display the age
print(age);
}
16. Multi-Line Comments
Multi-line comments start with /* and end with */.
They are useful when a comment needs multiple lines.
/*
This program demonstrates
basic Dart syntax and
program structure.
*/
void main() {
print("Hello Dart");
}
17. Documentation Comments
Documentation comments are commonly written using ///.
They can describe functions, classes, variables, and other APIs.
/// Displays a welcome message.
void showWelcome() {
print("Welcome to Dart");
}
Documentation comments are especially useful in larger projects because they can
provide useful information to developers using a class or function.
18. Comments Inside a Program
Comments can be placed before code or beside code when the explanation remains clear.
void main() {
int age = 25; // User's age
// Check whether the user is an adult
if (age >= 18) {
print("Adult");
}
}
19. Comments vs Code
Code |
Comment |
|---|
Executed by the program |
Used to explain code |
Produces program behavior |
Does not normally produce program behavior |
Must follow Dart syntax rules |
Can contain explanatory text |
Used to implement functionality |
Used to document functionality |
20. Whitespace and Formatting
Proper formatting makes Dart code easier to read and maintain. Spaces, indentation,
and line breaks should be used consistently.
Readable Code
void main() {
int age = 25;
if (age >= 18) {
print("Adult");
}
}
Less Readable Code
void main(){int age=25;if(age>=18){print("Adult");}}
Both examples represent the same basic logic, but the first format is much easier
for developers to read and maintain.
21. Case Sensitivity
Dart is case-sensitive. This means uppercase and lowercase letters are treated as different.
String name = "Rahul";
String Name = "Amit";
print(name);
print(Name);
name and Name are different identifiers.
22. Identifiers in Dart
Identifiers are names given to variables, functions, classes, and other program elements.
String studentName = "Rahul";
void calculateTotal() {
// Function code
}
class Student {
// Class code
}
Good Naming Examples
studentName
totalPrice
calculateAverage
userEmail
productList
Recommended Naming Style
Dart code commonly uses lower camel case for variables and functions.
String firstName = "Rahul";
int totalMarks = 450;
void calculateTotal() {
// Code
}
Classes are commonly written using UpperCamelCase.
class StudentProfile {
// Class members
}
23. Complete Example: Syntax, main(), Statements, and Comments
// Dart basic syntax example
/// Displays student information.
void displayStudent(String name, int age) {
print("Student Name: $name");
print("Student Age: $age");
}
void main() {
// Create variables
String name = "Rahul";
int age = 21;
// Display a message
print("Student Information");
// Call the function
displayStudent(name, age);
// Check the student's age
if (age >= 18) {
print("Status: Adult");
} else {
print("Status: Minor");
}
}
What This Example Demonstrates
// for single-line comments.
/// for documentation comments.
main() as the program entry point.
- Variable declarations.
- Function declaration.
- Function call.
if-else conditional statement.
print() function.
- Semicolons at the end of statements.
- Curly braces for code blocks.
24. Basic Dart Program Flow
A simple Dart program can be understood in the following order:
- The program starts execution.
- The
main() function is entered.
- Statements inside
main() are executed.
- Variables are created and values are assigned.
- Functions are called when required.
- Conditional statements or loops control execution when present.
- The program finishes when the required instructions have been executed.
void main() {
print("1. Program started");
int number = 10;
print("2. Number: $number");
print("3. Program finished");
}
25. Basic Syntax Rules to Remember
Concept |
Syntax |
Example |
|---|
Entry point |
void main() { } |
void main() { print("Hello"); } |
Statement |
statement; |
print("Hello"); |
Variable |
type name = value; |
int age = 25; |
Function |
returnType name() { } |
void greet() { } |
Condition |
if (condition) { } |
if (age > 18) { } |
Single-line comment |
// comment |
// User age |
Multi-line comment |
/* comment */ |
/* Description */ |
Documentation comment |
/// comment |
/// Displays user data. |
26. Common Beginner Mistakes
Missing Semicolon
Incorrect:
int age = 25
print(age);
Correct:
int age = 25;
print(age);
Incorrect Braces
Code blocks must have properly matched braces.
if (age >= 18) {
print("Adult");
}
Case-Sensitivity Mistake
String name = "Rahul";
print(Name);
The variable Name is different from name.
Forgetting Function Parentheses
Function calls require parentheses when calling a function without arguments.
greet();
27. Dart Syntax in Flutter
These basic Dart concepts are directly used when developing Flutter applications.
JustAcademy's curriculum places Dart language introduction in the introductory Flutter
module and Dart programming fundamentals before later topics such as widgets, navigation,
APIs, Firebase, and state management. :contentReference[oaicite:1]{index=1}
For example, a basic Flutter application uses Dart syntax like this:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("My Flutter App"),
),
body: const Center(
child: Text("Hello Flutter"),
),
),
);
}
}
This example demonstrates imports, the main() function, classes,
constructors, methods, parameters, statements, and comments—all of which are
built on Dart syntax.
28. Practice Exercises
- Write a Dart program that prints your name.
- Create a
main() function that prints three different messages.
- Create variables for name, age, and city and print them.
- Write a program that checks whether a person is an adult using
if-else.
- Add single-line comments to explain each statement in your program.
- Create a multi-line comment describing your Dart application.
- Create a function named
greetUser() and call it from main().
- Create a function that accepts a name and prints a personalized greeting.
- Write a small Dart program demonstrating variables, functions, conditions, and comments together.
29. Quick Revision
- Syntax: Rules for writing valid Dart code.
- main(): Standard entry point of a Dart application.
- Statement: An instruction executed by the program.
- Semicolon: Generally terminates Dart statements.
- Curly braces: Define blocks of code.
- //: Single-line comment.
- /* */: Multi-line comment.
- ///: Documentation comment.
- print(): Displays information in the console.
- Case-sensitive: Uppercase and lowercase identifiers are different.
30. Conclusion
Basic Dart syntax, the main() function, statements, and comments are the
foundation of Dart programming. Before developing complete Flutter applications, it is
important to understand how Dart code is structured, how execution begins from
main(), how statements are written, and how comments are used to document code.
These fundamentals provide the base for the broader Dart topics included in JustAcademy's
Flutter curriculum, such as variables, data types, operators, control statements,
functions, OOP, collections, and asynchronous programming. :contentReference[oaicite:2]{index=2}
Useful JustAcademy Links
Flutter Training:
JustAcademy Flutter Training
Course Demo Registration:
Register for Course Demo