Basic Syntax in Dart
Dart is a modern, object-oriented language with a simple and clean syntax. Here are some basic syntax elements of Dart to get you started:
1. Entry Point
Every Dart program starts with the main function:
void main() { print('Hello, World!'); }
2. Variables
Variables can be declared using var, final, or const
void main() { var name = 'Alice'; final age = 30; // Cannot be changed const pi = 3.14; // Compile-time constant print('Name: $name, Age: $age, PI: $pi'); }
Runtime Initialization: final variables are initialized at runtime. This means you can use runtime values to set them.
Compile-time Constant: A const variable is a compile-time constant. This means the value must be known and fixed at compile-time.
class CoffeeShop { // const is used for values that are known and fixed at compile-time static const int openingYear = 2020; // final is used for instance variables that are initialized once in the constructor final String name; final String size; final double price; // Constructor for initializing final variables CoffeeShop(this.name, this.size, this.price); void printCoffeeDetails() { print("Coffee: $name, Size: $size, Price: \$${price.toStringAsFixed(2)}, Opened in: $openingYear"); } } void main() { // Creating instances of CoffeeShop class var coffee1 = CoffeeShop("Latte", "Medium", 3.50); var coffee2 = CoffeeShop("Espresso", "Small", 2.00); // Printing coffee details coffee1.printCoffeeDetails(); coffee2.printCoffeeDetails(); }
3. Data Types
Dart supports various data types:
void main() { int integer = 10; double decimal = 10.5; String text = 'Hello'; bool isTrue = true; print('$integer, $decimal, $text, $isTrue'); }
4. Strings
Strings can be created using single or double quotes:
void main() { String singleQuote = 'Hello'; String doubleQuote = "World"; String combined = '$singleQuote $doubleQuote'; print(combined); }
5. Lists
Lists are ordered collections of items:
void main() { List<int> numbers = [1, 2, 3, 4, 5]; print(numbers); print(numbers[0]); // Access first element }
6. Maps
Maps are key-value pairs:
void main() { Map<String, int> ages = { 'Alice': 25, 'Bob': 30, }; print(ages); print(ages['Alice']); // Access value by key }
7. Functions
Functions can be declared with or without return types:
void main() { greet(); print('Sum: ${add(5, 3)}'); } void greet() { print('Hello!'); } int add(int a, int b) { return a + b; }
8. Control Flow Statements
If-Else
void main() { int number = 18; if (number > 0) { print('Positive'); } else { print('Non-positive'); } }
Loops
For Loop
void main() { for (int i = 0; i < 5; i++) { print(i); } }
While Loop
void main() { int i = 0; while (i < 5) { print(i); i++; } }
Switch Case
void main() { int number = 2; switch (number) { case 1: print('One'); break; case 2: print('Two'); break; default: print('Other'); } }
9. Classes
Dart is an object-oriented language with classes and objects
void main() { var person = Person('Alice', 30); person.greet(); } class Person { String name; int age; Person(this.name, this.age); void greet() { print('Hello, my name is $name and I am $age years old.'); } }
10. Async and Await
Dart has robust support for asynchronous programming:
import 'dart:async'; void main() async { print('Start'); await fetchData(); print('End'); } Future<void> fetchData() async { await Future.delayed(Duration(seconds: 2)); print('Data fetched'); }
These are the fundamental syntax elements to help you get started with Dart. Dart’s syntax is designed to be easy to read and write, making it a great choice for both beginners and experienced developers.