Dart in 5 minutes
Tags: dart, flutter, programming
Published: 2022-03-26 20:05:43

Dart is a free and open source language, powered by Google that aim to be a client-optimized for build fast app for all platforms.

---

## Hello Word
Legend has it that the best way to start is in this way.

```dart
void main()
{
    print("Hello World");
}

```

---

## Console Output
To print some in text in console, just use the code below.
```dart
print("some text here...");
```

In another way, you must import the package/library **dart:io** and use **stdout.write(“”)** function.
```dart
import 'dart:io';

void main() {
    stdout.write("Hello World");
}
```

---

## Console Input
First import dart:io in top of your code.
```dart
import 'dart:io';
```

Now use that function.
```dart
var input = stdin.readLineSync();
```

---

## Comments
Dart supports 3 types of comments.

### Single-line comment
In line start, use // to comment just one line.
```dart
// this is a simple comment
```

### Multi-line comment
To create a multi-line comment, start the comment block with /* and end with */
```dart
/* this is
multi-line comment
block */
```

### Documentation comment
Documentation comments is very useful, allow the IDE and text editor to orient the developers how they can use some feature created by you.
To start documentation comments, start the block with /// or /**
```dart
/// you can call
/// that method to make ...
```
Or
```dart
/***
this class store user data
*/
```

---

## Variable
To create variable you can just do as code below.
```dart
DataType variableName = value;
```

## Examples
```dart
// create a variable x
var x;
// assign some value to variable x
x = 18;
```
Or you can do all in one line.
```dart
// create a variable named y and assign some value
var y = 18;
```

In dart type is optionally, you can just create a variable with var keyword, or can can specify the type.
```dart
// variable of type integer
int z = 20;
// variable of type String
String name = "John";
```

**Dart supports many types of variables:**

**int**— store a integer number
```dart
int age = 18;
```

**double**— store a double precision number
```dart
double pi = 3.14;
```

**String**— store a text
```dart
String address = “Mindelo”;
```

**bool**— store booleans values
```dart
bool approved = true;
```

**List**— also know as array, store a list of values
```dart
List cars = [ "Honda", "Toyota" ];
```

**Set**— store a list of distinct values
```dart
Set islands = { "Santiago", "Fogo", "Brava" };
```

**Map**— store a object having key and values pair, both can be of any value
```dart
Map person = {
    "name": "Cesária",
    "last_name": "Évora",
    "profession": "Singer"
};
```

---

## Constants
Sometimes we want that variable values never change, for that we can use **const** and **final** keywords.
If you want a create a constant in compile-time use **const**, if you want to create a constant in runtime use **final**, for example you can make a user input as a constant using **final**.

```dart
// define a constant with name pi and value 3.14
const pi = 3.14;

// define a constant using final
final americaCapital = "Washington";

// define const from user input
final userName = stdin.readLineSync();
```

---

## Function
Below is a example to create a function to sum two numbers.
```dart
// function declaration
int sum(int x, int y)
{
    return x + y;
}
// call the function
int c = sum(4, 5);
// it will return the value of the sum 4 + 5
```

---

## Class
To declare a class you can do as the code below.
```dart
class Person
{
    String name;
    int age;
   Person(this.name, this.age );
}
```

Now just create a object from that class.
```dart
var someone = Person("David", 27);
```

---

## Conditions

### if
```dart
var age = 10;
if(age < 18)
{
    print("You are really young.");
}
else if(age < 30)
{
    print("You are 30.");
}
else
{
   print("You are adult.");
}
```

### switch…case
```dart
var animal = "dog";
switch(animal)
{
    case "dog":
        print("Aw aw awww");
    break;
    case "cow":
        print("Mu mu muuu");
    break;
    default:
        print("sound not found");
    break;
}
```

---

## Loops

### for
```dart
for(var i = 0; i < 10; i++)
{
    print(i);
}
```

### while
```dart
var i = 0;
while(i < 10)
{
    print(i);
    i++;
}
```

### do while
```dart
var i = 0;
do
{
    print(i);
    i++;
} while(i < 10);
```

---

## References
Photo by [Clem Onojeghuo](https://unsplash.com/@clemono?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)

[https://dart.dev/](https://dart.dev/)