#Dart

Dart Fundamentals I

A Dart programming language series to get up and running with dart and Flutter. Dart Fundamentals I.

· 9 min read
Dart Fundamentals I
Photo by Mauro Gigli / Unsplash

While we are studying Dart, it's important to keep the following facts in mind:

  • Everything in Dart is an object and every object is an instance of a class. Yes that's right, that includes: integers, functions and null. All objects, with the exception of null derived from the Object class.
  • Dart is strongly typed, yet type annotations are note required due to type inference.
  • If null safety is activated, variables can't be null, unless explicitly specified that they may be. Appending a question mark (?) to the end of a variable's type you make it nullable. The opposite can be said if we add an exclamation mark (!), if a variable is null and it shouldn't be, it will throw an exception.
  • Dart supports generic types, such as ListObject> (a list of any type of objects).
  • Dart allows top level functions such as main() and functions that are attached to a class or object such as instance and static methods. Functions within functions can create nested or local functions.
  • Dart lacks the public, private and protected keywords, but uses underscore (_) for reserved identifiers.

Variables

Variables keep track of references in a Dart program. A variable has a name or identifier and a value that it keeps track of. The variable type can be inferred by the variable's value, but it can be explicitly specified.

// Some variable examples
var greet = "Hello Hugo!";

// Specifying its type
String my_variable = "This is my variable";

// A variable can be dynamic, but we can specify the Object class to
// accept any value
Object name = "Hugo Marquez";

Variable Declaration

We can use the var keyword to declare a variable and with dart's type inference, it will automatically determine the variable data type based on its value. The syntax is as follow:

// Variable declaration
<type> <the variable name> = <values>;
var myVar = "A simple string";
String myString = "A simple string";

// Multipe variable declaration
<type> <var1, var2, var3>;
String str1, str2, str3;
int a,b,c;

Const and Final

We use final and const when we want to declare our variables as constants or we do not want to modify its value in the future. The const keyword is used to define compile-time constants.

// Final variables
final firstName = 'Hugo';
final String lastName = "Marquez";

// Const variables
const a = 10;

// Constant value
const uniqueValue = const [];

Default Values

When creating an uninitialized variable of the nullable type, they are created with the null value. Local variables do not need to be initialized when it is declared, but it must have a value before it is used. Top-level and class variables are lazily initialized, thus the initialization code is executed the first time the variable is needed.

// Nullable variable (numeric)
int? counter;
assert(counter == null); // this should assert to true

// If null safety is enabled, we first need to initialize
// the null value of a variable
int counter = 0;

Dart's Semicolon

In Dart, the semicolon is used to finalize or conclude a sentence, this to indicate that a statement has concluded. If it is not used correctly the compiler will throw an exception.

Dart's Keywords

There are 61 reserved keywords in the dart programming language, this keywords are reserved for the compiler and can't be used as a variable's identifier, class or function name. Keywords are case sensitive, so they must be written exactly as defined. This are some of the most common keywords in the dart programming language:

abstract Else import super
as Enum In switch
Assert export interface sync
async extends Is This
await extension Library throw
Break external mixin True
Case factory New Try
Catch False Null typedef
Class Final on1 Var
Const finally operator void
Continue For part while
covarient Function Rethrow with
Default get Return yield
deffered hide set
Do If show
dynamic implements static

Data Types

Numbers

Numbers are used to keep track of numeric values, in Dart there are two types of numeric data types:

  • Integers: where the value is a whole number or a non-fractional value. It provides a 64-bit non-decimal values that ranges from -263 to 263.
  • Doubles: a double represents 64-bit information with double precision for floating numbers.
// Integer examples
int a = 10;
int b = 35;

// Double examples
double c = 3.4;
double pi - 3.1416;

Number Properties

Properties Description
Hashcode Returns the hash code of the given number.
isFinite Returns true if the provided number is finite.
isInfinite Returns true if the number is infinite.
isNan Returns true if the number is not negative.
isNegative Returns true if the number is negative.
Sign Returns -1, 0 or 1 depending on the provided number sign.
isEven Returns true if the provided number is an even number.
isOdd Returns true if the given number is odd.

Number Methods

Method Description
abs() Returns the absolute value of a given number.
ceil() Returns the ceiling value of a given number.
floor() Returns the floor value of a given number.
compareTo() It compares the value with other number.
remainder() Returns the truncated remainder after dividing the two numbers.
round() Returns a round of the number.
toDouble() Returns the double equivalent representation of a number.
toInt() Returns the integer equivalent representation of a number.
toString() Returns the String equivalent representation of the number.
truncate() Returns integer after discarding the fraction digits.
parse() Converts a numeric string to a number.

Strings

A string is a sequence of characters that are stored on an array. In Dart strings can be declared with either a single or double quotation marks. A Dart string is made up of UTF-16 code units. Strings are immutable, which means they cannot be changeable after they are created.

// String examples
void main() {
  var hello = 'Hello World!';
  var message = "This string was declared with double 
  	quotation marks :)";
   
   var x = 100;
   var y = 30;
   
   // String interpolation, concatenation and printing
   String initial_str = "The sum of ";
   String x_str = "x: ${x} and ";
   String y_str = "y: ${y}";
   String result = "is ${x+y}";
   
   print(initial_str + x_str + y_str + result);
}

String Properties

Property Description
codeUnits Returns an unmodified list of UTF-16 code units of the provided string
isEmpty If the string is empty, it returns true.
Length Returns the length of the string, including whitespaces.

String Methods

Method Description
toLowerCase() It lowercases all characters in the specified string.
toUpperCase() It uppercases all characters in the provided string.
trim() It removes all whitespaces from the string.
compareTo() It compares one string with another.
replaceAll() Replaces all substrings that match the specified pattern with the string.
split() Devides the string at the supplied delimiter and returns a list of substrings.
substring() Returns the substring from start to end inclusive.
toString() Returns a string representation of a given object.
codeUnitAt() Returns the 16-bits code unit at the given index.

Booleans

The Booleans in Dart represents a true or false and the Boolean keyword is bool. The numeric values of 0 and 1 can't be used to represent true or false.

// Booleans
bool valid_response = true;
bool invalid_response = false;

Lists

In the dart programming language a list is a collection of ordered things and it is comparable to an array. The list's items are separated by a comma inside square brackets []. Lists are divided into two categories:

  • Fixed length lists: The length of the list is provided and it is not possible to change its size at runtime.
  • Growable lists: It is declared without specifying a size. At runtime it is possible to change it's size
void main() {
  // List is like an array
  var list = [1, 2, 3];
  
  // Fixed length list
  var fixed_list = new List(3);
  fixed_list[0] = 1;
  fixed_list[1] = 2;
  fixed_list[2] = 3;
  print(fixed_list);
  
  // Growable List
  var grow_list = new List();
  grow_list.add(1);
  grow_list.add(2);
  grow_list.add(3);
  print(grow_list);
  
  // The addAll()
  grow_list.addAll([1, 2, 3]);
  
  // The insert(index, value);
  grow_list.insert(2, 4);
  
  // The insertAll(index, iterable_list_of_values)
  grow_list.insertAll(3, [4, 5, 6, 7]);
  
  // The replaceRange(int start, int end, iterable)
  grow_list.replaceRange(1, 3, [5, 6, 7, 8]);
  
  // The remove(value)
  grow_list.remove(1);
  
  // The removeAt(index):
  grow_list.removeAt(2);
  
  // The removeLast()
  grow_list.removeLast();
  
  // The removeRange(int start, int end)
  grow_list.removeRange(0, 3);
  
  // List iteration
  grow_list.forEach((elements) {
    print("${grow_list.indexOf(elements)}: $elements"));
  });
}

List Properties

Property Description
First Returns the first element of the list.
isEmpty Returns true if the list is empty.
isNotEmpty Returns true if the list has at least one element
Length Returns the list's length
Last Returns the list's last element.
Reversed Returns a list in reverse order.
Single Whether the list has one element and returns it.

List Methods

Methods Description
add() Object is insterted at the end of the list.
addAll() Inserts several values into a list.
insert() Inserts an element at the provided index.
insertAll() Inserts multiple elements at the provided index.
replaceRange() Update list items within a defined range.
remove() Removes one element from the list at a time, it can recognize elements as arguments.
removeAt() Returns an element removed from the list at the provided index.
removeLast() Removes the last element of the list.

Sets

Sets in Dart are an unordered collection of elements of the same type. It's similar to an array, except it is not ordered. Sets do not allow us to insert duplicate values, thus the values on a set must be unique.

// Sets declaration
// var setName = <type>{};
// Set<type> setName = {};

var firstNames = <String>{'Hugo', 'John', 'Alex'};

// Accessing a set value at index 2
print(firstNames.elementAt(2));

// Finding a set by value
firstNames.contains('Hugo');

// Remove all elements of a set
firstNames.clear();

// Cast set to a list
// List<type> list_name = set_name.toList();
List<String> listFirstNames = firstNames.toList();

Set operations

Sets in the Dart programming language has the following operations:

  • Union: It merges the values of two provided sets.
  • Intersection: Returns the intersection between sets, all elements shared by both.
  • Subtraction: subtracts the elements (setA - setB), members of setB that are not present in setA are subtracted.

Maps

Map types are used to represent a key-value pair, where each key has a value associated with it. Any type of value and key can be used, but a key in a map must be unique. Curly braces are used to defined a map and a comma separates each pair.

// Map literal declaration
var employees = {'name': 'Hugo', 'last_name': 'Marquez', 'branch': 'Software Engineer'};

// Map constructor declaration
var employees = new Map();
employees['name'] = 'Hugo';
employees['last_name'] = 'Marquez';
employees['branch'] = 'Software Engineer';

// The Map.addAll(Map<key, value> other);
Map teachers = {'name': 'John', age: '30'};
teachers.addAll({'dept': 'Math', email:'john@school.org'});

// The Map.remove(object-key);
teachers.remove('age');

// Iterating over a map
// Map.forEach(void f(K key V value));
teachers.forEach((k, v) => print('${k} : ${v}'));

// Clearing a map
teachers.clear();

Map Properties

Property Description
Keys Returns all the keys as an iterable object.
Values Returns all values as an interable object.
Length Returns the length of the map object.
isEmpty If the map object contains no values, returns true.
isNotEmpty If the map object contains at least one value, returns true

Enums in Dart

Enumerators are a collection of known values as elements, members, etc. It's defined using the enum keyword and a comma separated list of valid identifiers. It is enclosed by curly braces and the syntax is as follows:

// Enums in dart
// enum <enum_name> {
//   const1,
//   const2,
//   const..n,
// }

enum months {
  Jan,
  Feb,
  March,
  April,
  May,
  June,
  July,
  Aug,
  Sept,
  Oct,
  Nov,
  Dec,
}

Runes

As defined previously, a string is a collection and sequence of characters of UTF-16 code units. Well a Rune is a unique string of Unicode UTF-32 code units, that enables us to express unique syntax. For example, the special character for a ❤️ is identical to the Unicode u2665, where u stands for Unicode and the digits are hexadecimal integers.

// Dart runes are UTF-32 code units
void main() {
  var heart_symb = '\u2665';
  print( heart_symb );
}

Symbols

A symbol in the dart programming language are used to specify an identifier or operator. In Dart the Hash (#) symbol is followed by the name of the identifier to define a Symbol. Symbols are used in dart as a reflection technique in combination with the mirror library.

import 'dart:core';
import 'dart:mirror';

void main() {
  Symbol library = new Symbol('my_lib');
  String name_of_libs = MirrorSystem.getName(library);
}

What's next?

For Part II we will use the above knowledge and continue our journey on the dart programming language with control flow statements, constants, switch-cases, conditional statements and dart functions.

Cheers! 🍻


Resources

Great news! The dart and flutter ecosystem is very well documented and full of examples and tutorials all over the internet. Here is a list, to mention a few, of useful resources:

Dart programming language
Dart is a client-optimized language for fast apps on any platform
Mastering Flutter: A Beginner’s Guide (Mastering Comput…
Discover and share books you love on Goodreads.
Reflective programming - Wikipedia

Related Articles

Flutter vs Javascript

Flutter vs Javascript

· 2 min read
The Dart Programming Language

The Dart Programming Language

· 5 min read