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 Worl…


This content originally appeared on DEV Community and was authored by Assis Ferreira

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");
}

Console Output

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

print("some text here...");

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");
}

Console Input

First import dart:io in top of your code.

import 'dart:io';

Now use that function.

var input = stdin.readLineSync();

Comments

Dart supports 3 types of comments.

Single-line comment

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

// this is a simple comment

Multi-line comment

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

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

/// you can call
/// that method to make ...

Or

/***
this class store user data
*/

Variable

To create variable you can just do as code below.

DataType variableName = value;

Examples

// create a variable x
var x;
// assign some value to variable x
x = 18;

Or you can do all in one line.

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

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

int age = 18;

double— store a double precision number

double pi = 3.14;

String— store a text

String address = Mindelo;

bool— store booleans values

bool approved = true;

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

List cars = [ "Honda", "Toyota" ];

Set— store a list of distinct values

Set islands = { "Santiago", "Fogo", "Brava" };

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"
};

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();

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

Class

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

class Person
{
    String name;
    int age;
   Person(this.name, this.age );
}

Now just create a object from that class.

var someone = Person("David", 27);

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.");
}

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;
}

Loops

for

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

while

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

do while

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

References

Photo by Clem Onojeghuo on Unsplash

https://dart.dev/


This content originally appeared on DEV Community and was authored by Assis Ferreira


Print Share Comment Cite Upload Translate Updates
APA

Assis Ferreira | Sciencx (2022-03-26T20:05:43+00:00) Dart in 5 minutes. Retrieved from https://www.scien.cx/2022/03/26/dart-in-5-minutes/

MLA
" » Dart in 5 minutes." Assis Ferreira | Sciencx - Saturday March 26, 2022, https://www.scien.cx/2022/03/26/dart-in-5-minutes/
HARVARD
Assis Ferreira | Sciencx Saturday March 26, 2022 » Dart in 5 minutes., viewed ,<https://www.scien.cx/2022/03/26/dart-in-5-minutes/>
VANCOUVER
Assis Ferreira | Sciencx - » Dart in 5 minutes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/26/dart-in-5-minutes/
CHICAGO
" » Dart in 5 minutes." Assis Ferreira | Sciencx - Accessed . https://www.scien.cx/2022/03/26/dart-in-5-minutes/
IEEE
" » Dart in 5 minutes." Assis Ferreira | Sciencx [Online]. Available: https://www.scien.cx/2022/03/26/dart-in-5-minutes/. [Accessed: ]
rf:citation
» Dart in 5 minutes | Assis Ferreira | Sciencx | https://www.scien.cx/2022/03/26/dart-in-5-minutes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.