Learn Dart from Scratch
Hello! Welcome
Today we are going to learn about Dart from Scratch
What is Dart?
Dart is a programming language created by Google. It is simple, flexible, and designed to create modern applications.
Dart is known for its fast performance and clear syntax. It makes development easier for both mobile and web applications.
Why learn Dart?
Dart is the language behind Flutter, the popular tool for creating mobile, web, and desktop applications.
This has made it a favorite option for many programmers. It’s perfect for modern developers!
Installing Dart
To get started, install the Dart SDK from the official page.
- Download the SDK from dart.dev.
- Follow the installation steps.
You can also install it with Flutter, which includes Dart.
You’re doing great!
Now let’s talk about the syntax of Dart.
Variables and Data Types
In Dart, you can use variables to store data. Data types include integers, floats, strings, and booleans.
var name = 'Dart'; // String type
int age = 10; // Integer type
var total = age + 5; // Operation
With these, you can perform arithmetic, logical, and comparison operations:
Conditionals
With conditionals, you can make the program make decisions. We use if
and else
to execute code only if a condition is true.
For example:
if (age > 18) {
print('Adult');
}
Loops
Loops allow us to repeat actions and iterate over ranges and collections. For example, for
, while
, and do-while
.
for (var i = 0; i < 5; i++) {
print(i); // Prints from 0 to 4
}
Functions in Dart
Functions are blocks of code that perform specific tasks.
void greet() {
print('Hello, LuisLlamas.es!');
}
Functions help to organize and reuse code.
Collections
Collections are structures that allow you to store multiple values. Lists and maps are the most common.
var numbers = [1, 2, 3];
var capitals = {'Colombia': 'Bogotá', 'Mexico': 'CDMX'};
Collections make it easy to handle groups of data.
Classes and Objects
Dart is an object-oriented language. You can create classes and objects to model data and behaviors in your application.
Classes organize data and behaviors, while objects are instances of those classes.
You’re almost there!
We just need to see how to create a project in Dart.
Creating a Project in Dart
To start a project in Dart, open your terminal or command line and type the following:
dart create my_project
You now have a project structure ready to start coding!
Your First Program in Dart
Inside the project, open the file lib/main.dart
. Here we will write our first program in Dart:
void main() {
print('Hello, LuisLlamas.es!');
}
A great first step to get familiar with the language!
Compile and Run
To compile and run your program, we simply need to run;
dart run
If everything is correct, you will see Hello, LuisLlamas.es! in the console. 🎉
Well done!
You already know the basics of Dart! Keep exploring the language.