Flutter Development Fundamentals
Introduction to Dart
Your First Look at Dart
Before building apps with Flutter, you need to understand its language: Dart. Dart is a programming language developed by Google, designed to be clear, concise, and easy to learn. It powers everything you'll do in Flutter.
Let's start with the classic first program everyone writes. It simply prints "Hello, World!" to the screen.
// Every Dart program starts with the main() function.
void main() {
print('Hello, World!');
}
Let's break this down. Every Dart program needs a main() function; it's the entry point where your code begins to run. The print() function is a simple way to display output. Notice the text is wrapped in single quotes, and the line ends with a semicolon. Semicolons are how Dart knows a statement is finished.
Storing Information
Programs need to work with data, like numbers, text, and true/false values. We store this data in variables. Think of a variable as a labeled box where you can keep a piece of information. Each variable has a type that tells Dart what kind of data it can hold.
| Type | Description | Example |
|---|---|---|
int | Integers (whole numbers) | 42 |
double | Numbers with decimal points | 3.14 |
String | A sequence of characters | 'Hello' or "Hello" |
bool | A true or false value | true or false |
You can declare a variable by stating its type, followed by its name. Or, you can use the keyword var, and Dart will figure out the type for you based on the value you give it. This is called type inference.
// Dart can infer the type with 'var'.
var name = 'Ada Lovelace';
var year = 1815;
// Or you can specify the type explicitly.
String language = 'Dart';
double version = 3.0;
Sometimes, you'll have a variable that shouldn't change after it's first assigned. For this, you can use final or const. Use final for a variable whose value is set once. Use const if the value is known when you write the code (a compile-time constant).
final String greeting = 'Welcome';
const double pi = 3.14159;
Making Decisions and Repeating Actions
Programs aren't just lists of instructions executed one after another. They need to make decisions and perform repetitive tasks. Dart uses control structures to manage this flow.
To make a decision, you use an if statement. It checks if a condition is true and runs a block of code if it is. You can add an else block to run code if the condition is false.
int temperature = 25;
if (temperature > 20) {
print('It\'s a warm day!');
} else {
print('It might be a bit chilly.');
}
To repeat an action multiple times, you can use a loop. A for loop is great when you know how many times you want to repeat something. This example counts from 1 to 3.
for (int i = 1; i <= 3; i++) {
print('Launch sequence: $i');
}
// Output:
// Launch sequence: 1
// Launch sequence: 2
// Launch sequence: 3
The
$isyntax inside the string is called string interpolation. It's a handy way to insert the value of a variable directly into a string.
Now let's check your understanding of these core concepts.
Ready for a few questions?
What is the function that serves as the starting point for every Dart application?
What is the primary purpose of a semicolon (;) in Dart?
With these basics of syntax, variables, and control flow, you have the foundation needed to start building with Dart. Next, you'll see how these concepts apply within the Flutter framework.