Dart Fundamentals: Basic Syntax and Data Types for Flutter
Welcome to the foundational elements of Dart, the programming language that powers Flutter. Understanding Dart's basic syntax and data types is crucial for building robust and efficient Flutter applications. This module will guide you through the essential building blocks of Dart.
Your First Dart Program
Every Dart program begins with a
main()
print()
The main()
function.
Here's a simple example:
void main() {print('Hello, Dart!');}
Variables and Data Types
Variables are containers for storing data values. Dart is a statically typed language, meaning you declare the type of a variable. However, Dart also supports type inference, where the compiler can often deduce the type.
Dart uses static typing and type inference for variables.
Variables hold data. You can explicitly declare their type (like int
for integers) or let Dart figure it out.
In Dart, you declare variables using keywords like var
, final
, and const
. var
allows the variable's type to be inferred and can be reassigned. final
means the variable can only be assigned once, and const
means it's a compile-time constant. Explicitly typing variables, such as int count = 10;
or String name = 'Alice';
, is also common and recommended for clarity.
Common Data Types
Type | Description | Example |
---|---|---|
int | Whole numbers (e.g., -1, 0, 100) | <code>int age = 30;</code> |
double | Floating-point numbers (e.g., 3.14, -0.5) | <code>double price = 19.99;</code> |
String | Sequences of characters (text) | <code>String message = 'Welcome';</code> |
bool | Boolean values (true or false) | <code>bool isActive = true;</code> |
List | Ordered collection of elements (like arrays) | <code>List<int> numbers = [1, 2, 3];</code> |
Map | Unordered collection of key-value pairs | <code>Map<String, int> scores = {'Alice': 95, 'Bob': 88};</code> |
Operators
Operators are symbols that perform operations on variables and values. Dart supports arithmetic, relational, logical, and assignment operators.
Operators perform actions on data.
Operators like +
, -
, *
, /
, ==
, !=
, &&
, ||
are used to manipulate and compare data.
Arithmetic operators include +
(addition), -
(subtraction), *
(multiplication), /
(division), and %
(modulo). Relational operators like ==
(equal to), !=
(not equal to), >
(greater than), <
(less than), >=
(greater than or equal to), and <=
(less than or equal to) are used for comparisons. Logical operators &&
(AND), ||
(OR), and !
(NOT) combine boolean expressions. Assignment operators like =
and +=
are used to assign values.
The ==
operator.
Control Flow Statements
Control flow statements allow you to execute different blocks of code based on certain conditions or to repeat code execution.
Loading diagram...
Common control flow statements include
if-else
for
while
switch
Understanding these fundamental syntax elements and data types is the bedrock of your journey into Flutter development. Practice writing small Dart programs to solidify your grasp!
Learning Resources
The official and comprehensive guide to Dart's syntax, data types, and core features.
While this link is about state management, Flutter's official documentation often links to or embeds Dart fundamentals relevant to app development.
A quick and engaging video tutorial that covers the essential Dart syntax and concepts.
A detailed video walkthrough of Dart's basic syntax, including variables, data types, and operators.
A beginner-friendly blog post explaining Dart's core syntax and common data types with clear examples.
GeeksforGeeks provides a clear explanation of Dart's built-in data types and their usage.
Official documentation detailing all the operators available in Dart and how they function.
A helpful article clarifying the differences and use cases for Dart's variable declaration keywords.
A tutorial covering Dart's conditional statements (`if-else`, `switch`) and loops (`for`, `while`).
Provides a general overview of the Dart language, its history, and key features.