Dart syntax forms the foundation of writing clean, efficient code in the Dart programming language. Understanding these fundamental rules is crucial for developers working with Dart or Flutter.
Dart programs typically start with a main()
function, the entry point of execution. Here's a simple example:
void main() {
print('Hello, Dart!');
}
Dart uses Dart Variables to store data. You can declare variables using var
, or specify the type explicitly:
var name = 'John';
String surname = 'Doe';
int age = 30;
double height = 1.75;
For more details on data types, check out Dart Data Types.
Functions in Dart are declared using the following syntax:
returnType functionName(parameterType parameter) {
// Function body
return value;
}
For a deeper dive into functions, visit Dart Function Basics.
Dart provides various control flow statements:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Learn more about conditional statements in Dart If-Else Statements.
Dart supports several types of loops:
// For loop
for (var i = 0; i < 5; i++) {
print(i);
}
// While loop
while (condition) {
// Code to execute
}
// Do-while loop
do {
// Code to execute
} while (condition);
Explore more about loops in Dart For Loops and Dart While Loops.
Dart is an object-oriented language. Here's a basic class structure:
class Person {
String name;
int age;
Person(this.name, this.age);
void sayHello() {
print('Hello, my name is $name');
}
}
For more on object-oriented programming in Dart, check out Dart Classes and Dart Objects.
By mastering Dart syntax, you'll be well-equipped to write efficient, readable code for various applications, including Flutter development.