Introduction to Dart

In this section, you will learn how the basics of the Dart programming language. We will take a look at its syntax and learn how to use data types like strings and integers. We will also look at how to perform string interpolation, how to build collections, and how to create functions.



Downloads

To follow this course, you can download the source file, which will help you compare your progress.

Source Code

You can also view the source code for each section in the course in my GitHub repository.

Getting Started

The code we will be writing is not directly linked to the project we are building but it's still good to gain a practice. Selecting the top-level folder (mine is simply called designcode), press CMD+N on your keyboard and select Dart file. Name this file scratch.dart. You should have a new empty Dart file now that can be used as a playground to test and run code. Inside the file, type the following:


void main() {
}


This main function will let Dart know that this is the entry point function to run. The void keyword means that this function will return nothing.
Flutter_S03_01

Variables

Variables can be thought of as boxes that hold a particular value. Below, I'll define a variable called name that will hold a String value of my name. Then, I will print it to the console.

void main() {
var name = 'Sai';
print(name);
}


To run code, click on the Terminal menu at the bottom of Android Studio and run the following:

dart run scratch.dart

Flutter_S03_02

Keywords

  • Dynamic: This keyword lets Flutter know that even though name contains a value of type String, it can be allowed to change to a value of a different data type.

  • Final: This keyword lets Flutter know that this variable cannot be changed later on making the variable immutable. Final is initialized at run-time meaning that every time the state of the app reloads, so do the variables marked final.

  • Const: This is similar to the final keyword. Const is initialized at compile-time so when the code compiles, the variable is initialized and is never reinitialized again.

Note: You can also mark variables by the data type they hold. For example, the name variable can be marked String to let the code know this variable should always hold a value of type String.


final String name = 'Sai';
print(name);

Flutter_S03_03

Integer Data Type


Setting a variable to hold a value of type integer is very simple. All it takes is marking a variable int and setting its value.

int age = 19;


String Interpolation


String Interpolation refers to creating string objects which can dynamically be changed by including variables within the string.

print('My name is $name and my age is $age!')

Flutter_S03_04

Collections

Many programming languages include some form of collections. While Swift has arrays, Dart, similar to Python, has lists. To create a list, set a variable equal to a multiple objects of the same data type wrapped in square brackets and separated by commas.


var hobbies = <String>['coding', 'biking', 'reading'];

Flutter_S03_05

Functions

Functions can be handy when you want to perform a particular operation repeatedly without having to write the same lines of codes over and over again. Let's delete everything inside the main function. We'll create a showInfo function that simply prints a statement to the console.

void main() {
void showInfo() {
print('My name is Sai and I am 19 years old');
}
showInfo();
}


If we run the Terminal command dart run scratch.dart, we should see the statement printed to the console.
Flutter_S03_06

Functions with Arguments

We can customize our functions by providing arguments. This can customize small portions of our function to change based on the arguments provided. We'll add an argument called name that will be used to change the print statement in our function.

void showInfo({String name}) {
print("My name is $name and I am 19 years old");
}

showInfo(name: 'Sai');

Flutter_S03_07

Functions that return values

Let's remove everything inside the main function for the last time. We'll now create a function that will take a number as an argument and return the value of that number plus 5. To make a function return a value, we have to replace the void keyword with the data type we expect the function to return.

void main() {
int addFive({int number}) {
return 5 + number;
}
print('5 + 5 is ${addFive(number: 5)}');
}


We used the int keyword since we want our functions to return an integer value. Now if we run the Dart code, the arithmetic operation will be printed to the console.
Flutter_S03_08