Hello! I think that before installing Flutter and using it we need to explain the Dart language. If you haven't already read the first post of this series ("What's Flutter?") go read it with this link.
If you're already familiar with JavaScript or C it will be a lot easier.
You can follow the examples on the online Dart compiler:
Variables
The declaration of variables follows this path:
// (modificator) (type) name = value;
final int points = 25;
Primitive types
In Dart, there are 4 primitive types: bool
, int
, double
and num
:
void main() {
bool yes = true;
bool no = false;
int integer = 25;
double decimal = 3.14;
num number = 24601;
print(integer);
print(decimal);
print(number);
print(yes);
print(no);
}
>> 25
>> 3.14
>> 24601
>> true
>> false
Dynamic Type
In Dart we have something similar to the any
type in TypeScript, it's called dynamic
. It acts like a bypass and can accept every value:
void main() {
dynamic data = "Hello";
print(data);
print(data.runtimeType); // prints the type of the variable
}
>> "Hello"
>> "String"
Final & Const
Constants in Dart can be of two types: final
or const
. final
variables are runtime constants while const
variables are compile-time constants.
void main() {
final int myCurrentAge;
myCurrentAge = 22;
const double pi = 3.14;
print(myCurrentAge);
print(pi);
}
>> "22"
>> "3.14"
Strings
In Dart, to define a String we can use both ' and ", in case we use the apix (') and in our expression, there's another ', we can pre-follow this by the backslash "\":
void main() {
final myName = "Nick";
final signText = 'Don\'t pollute here!'; // here we use \ with '
print(myName);
print(signText);
}
>> "Nick"
>> "Don't pollute here!"
Multiple-lines Strings
Dart offers 2 different methods to define a multiple-lines string:
Use the new line character when needed (
\n
);Use the
'''
annotation and go to a new line when needed;
Here's a usage:
void main() {
final String normal = 'First Line \nSecond Line \nThird Line';
final String ceasar = '''I came,
I saw,
I conquered''';
print(normal);
print("");
print(ceasar);
}
>> "
First Line
Second Line
Third Line
"
>> "
I came,
I saw,
I conquered
"
Concatenation and Interpolation
In Dart, concatenation between strings can be done with the plus symbol '+':
void main() {
final String firstName = 'Robert';
final String surname = 'Williams';
print(firstName + ' ' + surname);
// the same as:
final String fullName = firstName + ' ' + surname;
print(fullName);
}
>> "Robert Williams"
>> "Robert Williams"
Interpolation can be done by adding to the string the dollar symbol and then the variable to include. If the element to include is more complex than a single variable, we can enclose it between curly brackets:
void main() {
final int currentYear = 2023;
print("We are currently in the year $currentYear");
print("The next year will be ${currentYear + 1}");
}
>> "We are currently in the year 2023"
>> "The next year will be 2024"
Collections
Dart uses 3 object classes to structure data: List
, Map
and Set
.
List
Lists are like arrays in other programming languages. We would use Lists like this:
void main() {
final List<int> numbersBelowTen= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// List<Type> name = [value];
// now we print the values with two different methods
print("Here are the numbers below ten:");
for (int i = 0; i < numbersBelowTen.length; i++) {
print(numbersBelowTen[i]);
}
print("");
print("Here are the same numbers:");
numbersBelowTen.forEach((int number) => print(number));
}
>> "Here are the numbers below ten:
0
1
2
3
4
5
6
7
8
9"
>> "Here are the same numbers:
0
1
2
3
4
5
6
7
8
9"
To add something to a List we use the add
method:
void main() {
List<String> clients = [];
clients.add("Robert");
clients.add("Erick");
clients.add("Adam");
print(clients);
}
>> "[Robert, Erick, Adam]"
Map
Maps are structures where data is stored in a non-linear association way. In maps, we find, for every element, its key
which is what we're gonna use to identify the element in its Map. Here's how we could use them:
void main() {
Map<String, int> carPaintsPrices = {
'Black': 50,
'Grey': 50,
'White': 50,
'Blue': 70,
'Green': 80,
'Red': 60,
'Yellow': 90,
'Lime': 90,
};
print("The Blue paint costs \$${carPaintsPrices['Blue']}");
print("");
print("All the paints below \$70:");
carPaintsPrices.forEach((color, cost) {
if (cost < 70) {
print("$color: \$$cost");
}
});
}
>> "The Blue painting costs $70"
>> "All the paintings below $70:
Black: $50
Grey: $50
White: $50
Red: $60"
Set
Sets are structures where data is stored in a non-linear order. The particularity of this structure is that duplicates get ignored, so we can use it to eliminate them from a list:
void main() {
List<int> listWithDuplicates = [2, 5, 5, 1, 2, 6];
// we convert the list to set to eliminate the duplicates
Set<int> convertedList = Set.from(listWithDuplicates);
// now we re-convert the set to list to get a list with no duplicates
List<int> finalList = convertedList.toList();
print("The initial list without duplicates: $finalList");
}
>> "The list without duplicates: [2, 5, 1, 6]"
For now, this is all you need to start with Flutter. In the next post, we will install Flutter both on Windows and Mac machines.