DEV Community

Cover image for Dart in 5 minutes
Assis Ferreira
Assis Ferreira

Posted on

Dart in 5 minutes

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.

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

Enter fullscreen mode Exit fullscreen mode

Console Output

To print some in text in console, just use the code below.

print("some text here...");
Enter fullscreen mode Exit fullscreen mode

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

import 'dart:io';

void main() {
    stdout.write("Hello World");
}
Enter fullscreen mode Exit fullscreen mode

Console Input

First import dart:io in top of your code.

import 'dart:io';
Enter fullscreen mode Exit fullscreen mode

Now use that function.

var input = stdin.readLineSync();
Enter fullscreen mode Exit fullscreen mode

Comments

Dart supports 3 types of comments.

Single-line comment

In line start, use // to comment just one line.

// this is a simple comment
Enter fullscreen mode Exit fullscreen mode

Multi-line comment

To create a multi-line comment, start the comment block with /* and end with */

/* this is
multi-line comment
block */
Enter fullscreen mode Exit fullscreen mode

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 /**

/// you can call
/// that method to make ...
Enter fullscreen mode Exit fullscreen mode

Or

/***
this class store user data
*/
Enter fullscreen mode Exit fullscreen mode

Variable

To create variable you can just do as code below.

DataType variableName = value;
Enter fullscreen mode Exit fullscreen mode

Examples

// create a variable x
var x;
// assign some value to variable x
x = 18;
Enter fullscreen mode Exit fullscreen mode

Or you can do all in one line.

// create a variable named y and assign some value
var y = 18;
Enter fullscreen mode Exit fullscreen mode

In dart type is optionally, you can just create a variable with var keyword, or can can specify the type.

// variable of type integer
int z = 20;
// variable of type String
String name = "John";
Enter fullscreen mode Exit fullscreen mode

Dart supports many types of variables:

int— store a integer number

int age = 18;
Enter fullscreen mode Exit fullscreen mode

double— store a double precision number

double pi = 3.14;
Enter fullscreen mode Exit fullscreen mode

String— store a text

String address = Mindelo;
Enter fullscreen mode Exit fullscreen mode

bool— store booleans values

bool approved = true;
Enter fullscreen mode Exit fullscreen mode

List— also know as array, store a list of values

List cars = [ "Honda", "Toyota" ];
Enter fullscreen mode Exit fullscreen mode

Set— store a list of distinct values

Set islands = { "Santiago", "Fogo", "Brava" };
Enter fullscreen mode Exit fullscreen mode

Map— store a object having key and values pair, both can be of any value

Map person = {
    "name": "Cesária",
    "last_name": "Évora",
    "profession": "Singer"
};
Enter fullscreen mode Exit fullscreen mode

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.

// 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();
Enter fullscreen mode Exit fullscreen mode

Function

Below is a example to create a function to sum two numbers.

// 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
Enter fullscreen mode Exit fullscreen mode

Class

To declare a class you can do as the code below.

class Person
{
    String name;
    int age;
   Person(this.name, this.age );
}
Enter fullscreen mode Exit fullscreen mode

Now just create a object from that class.

var someone = Person("David", 27);
Enter fullscreen mode Exit fullscreen mode

Conditions

if

var age = 10;
if(age < 18)
{
    print("You are really young.");
}
else if(age < 30)
{
    print("You are 30.");
}
else
{
   print("You are adult.");
}
Enter fullscreen mode Exit fullscreen mode

switch…case

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;
}
Enter fullscreen mode Exit fullscreen mode

Loops

for

for(var i = 0; i < 10; i++)
{
    print(i);
}
Enter fullscreen mode Exit fullscreen mode

while

var i = 0;
while(i < 10)
{
    print(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

do while

var i = 0;
do
{
    print(i);
    i++;
} while(i < 10);
Enter fullscreen mode Exit fullscreen mode

References

Photo by Clem Onojeghuo on Unsplash

https://dart.dev/

Top comments (0)