In this section, we will cover the fundamental syntax and structure of Dart programs. Understanding these basics is crucial for writing efficient and error-free Dart code.
Key Concepts
- Dart Program Structure
- Comments
- Variables and Data Types
- Functions
- Control Flow Statements
- Error Handling
- Dart Program Structure
A Dart program typically consists of:
- Libraries: Collections of related classes and functions.
- Classes: Blueprints for creating objects.
- Functions: Blocks of code that perform specific tasks.
- Variables: Containers for storing data values.
Example
// Importing a library import 'dart:math'; // Defining a class class Circle { // Class properties double radius; // Constructor Circle(this.radius); // Method to calculate area double area() { return pi * radius * radius; } } // Main function void main() { // Creating an object of Circle Circle circle = Circle(5.0); print('The area of the circle is ${circle.area()}'); }
Explanation
- Importing Libraries:
import 'dart:math';
imports the math library. - Class Definition:
class Circle
defines a class namedCircle
. - Constructor:
Circle(this.radius);
initializes theradius
property. - Method:
double area()
calculates the area of the circle. - Main Function:
void main()
is the entry point of the program.
- Comments
Comments are used to explain code and are ignored by the compiler.
- Single-line comments: Use
//
- Multi-line comments: Use
/* ... */
- Documentation comments: Use
///
or/** ... */
Example
// This is a single-line comment /* This is a multi-line comment */ /// This is a documentation comment /// for a function void exampleFunction() { // Function code here }
- Variables and Data Types
Variables store data values. Dart is a statically-typed language, meaning you must declare the type of a variable.
Common Data Types
Data Type | Description | Example |
---|---|---|
int |
Integer values | int age = 30; |
double |
Floating-point values | double height = 5.9; |
String |
Sequence of characters | String name = 'John'; |
bool |
Boolean values (true/false) | bool isActive = true; |
var |
Type inferred by the compiler | var score = 100; |
Example
void main() { int age = 30; double height = 5.9; String name = 'John'; bool isActive = true; var score = 100; // Type inferred as int print('Name: $name, Age: $age, Height: $height, Active: $isActive, Score: $score'); }
- Functions
Functions are blocks of code that perform specific tasks. They can take parameters and return values.
Syntax
Example
int add(int a, int b) { return a + b; } void main() { int result = add(5, 3); print('The sum is $result'); }
- Control Flow Statements
Control flow statements determine the flow of execution in a program.
If-Else Statement
void main() { int number = 10; if (number > 0) { print('Positive number'); } else if (number < 0) { print('Negative number'); } else { print('Zero'); } }
For Loop
While Loop
- Error Handling
Dart provides a way to handle errors using try
, catch
, and finally
blocks.
Example
void main() { try { int result = 10 ~/ 0; // Integer division by zero print(result); } catch (e) { print('Error: $e'); } finally { print('This is always executed'); } }
Practical Exercises
Exercise 1: Basic Dart Program
Task: Write a Dart program that defines a class Rectangle
with properties length
and width
, and a method area
that calculates the area of the rectangle. Create an object of Rectangle
and print the area.
Solution:
class Rectangle { double length; double width; Rectangle(this.length, this.width); double area() { return length * width; } } void main() { Rectangle rectangle = Rectangle(5.0, 3.0); print('The area of the rectangle is ${rectangle.area()}'); }
Exercise 2: Control Flow
Task: Write a Dart program that takes an integer input and prints whether the number is even or odd.
Solution:
void main() { int number = 4; if (number % 2 == 0) { print('$number is even'); } else { print('$number is odd'); } }
Summary
In this section, we covered the basic syntax and structure of Dart programs, including:
- Program structure
- Comments
- Variables and data types
- Functions
- Control flow statements
- Error handling
Understanding these basics will help you write more complex Dart programs as you progress through the course. In the next module, we will dive deeper into Dart basics, starting with variables and data types.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure