What is Dart? 🚀
Dart is a modern programming language created by the company Google. It is used for development mobile applications (Flutter), web applications, as well as server programs. Dart was developed with a focus on simplicity, speed and safety.
Why study Dart? 🔥
Simplicity: Dart has a clear and clean syntax that is easy to learn.
Performance: The code is compiled into machine code, which makes programs fast.
Cross-platform: Works on the web, mobile devices and servers.
Asynchrony: Support for
asyncandawaitmakes it easy to work with multitasking.
How to install Dart? 💻
To work with Dart, you will need:
✅ Install Dart SDK. You can download it from official website.
✅ Text editor, for example Visual Studio Code with the Dart extension.
✅ Online editor, if you don't want to install Dart locally, you can use DartPad.
How to check if Dart is installed? 🛠
Open the terminal (or command line) and enter:
dart --versionIf Dart is installed, you will see its version.
Dart Syntax Basics 📚
1. Variables 📦
In Dart, variables can be declared using var, final and const. The difference between them:
var— variable.final— the value can be set only once.const— constant, calculated during compilation.
Example of declaring variables:
void main() {
var name = "Alice"; // modifiable variable
final age = 25; // constant value, cannot be changed
const pi = 3.14; // constant, set at the compilation stage
print("Name: $name, Age: $age, PI Number: $pi");
}2. Data output 📤
The display is carried out using print().
Example of output:
void main() {
print("Hello, world!");
}3. Conditional operators (if...else) 🧐
Using conditions, you can check data and change the behavior of the program.
Example of using conditions:
void main() {
int age = 20;
if (age >= 18) {
print("You're an adult!");
} else {
print("You're still a child.");
}
}4. Loops (for, while) 🔄
Loops allow you to perform the same action multiple times.
Example: output of numbers from 1 to 5
void main() {
for (int i = 1; i <= 5; i++) {
print(i);
}
}5. Functions 📐
Functions allow you to break the code into logical blocks.
Example of a function for calculating the square of a number:
int square(int x) {
return x * x;
}
void main() {
print("Square of 5: ${square(5)}");
}Simple projects in Dart 💡
1. Random number generator 🎲
The program generates a random number from 0 to 99 and displays it on the screen.
import 'dart:math';
void main() {
var random = Random();
print("Random number: ${random.nextInt(100)}");
}2. Multiplication table 📊
This code displays a multiplication table from 1 to 10.
void main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
print("$i x $j = ${i * j}");
}
print(""); // Empty line for separation
}
}3. Character counter in a line 🔢
The program counts the number of characters in a given string.
void main() {
String text = "Hello, Dart!";
print("Number of characters in the line: ${text.length}");
}4. Checking the number for evenness 🔍
The program determines whether the number is even or odd.
void main() {
int number = 42;
if (number % 2 == 0) {
print("$number - even number");
} else {
print("$number - odd number");
}
}5. Calculating the factorial of a number 🎯
This code calculates the factorial of a given number.
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
void main() {
print("Factorial 5: ${factorial(5)}");
}Conclusion 🎉
Dart is a powerful and convenient language for creating mobile and web applications. We have analyzed its basics: variables, input/output, conditions, loops and functions. Now you can write your first programs! 🚀
The more you practice, the better your code becomes. Experiment, try new tasks and master programming! Good luck learning Dart! 😊
