Dart Basics

Dart Basics

Introduction to Dart, data types, variables and scope.

Any programming language has to deal with data in order to perform computations and logic. Data comes in various types, and programming languages provide ways to represent and manipulate that data.

Data types define the kind of values that a variable can store and the operations that can be performed on that data. Some of the basic data types are:

  • Numbers: Integer and floating point numbers. Languages define numeric types like int, float, double etc. Computers store numeric data by representing them in binary form.

  • Characters: Represent individual letters, symbols and control characters. Languages define character types like char. Computers store characters using encoding schemes like ASCII.

  • Booleans: Represent logical true/false values. Languages define boolean types like bool. Computers store booleans as 1 or 0.

  • Strings: Ordered sequences of characters. Languages define string types like String, varchar etc. Computers store strings by storing the address of the first character in memory.

When a program is executed, the computer allocates memory to store the data associated with variables. The amount of memory allocated depends on the data type.


Dart Data Types

Dart has a robust type system to represent and handle data. The main data types in Dart are:

  • Numbers: Dart supports int for integers and double for floating point numbers.

  • Booleans: Dart has a boolean type to represent true/false values.

  • Strings: Dart has String type to represent text data. Strings are immutable in Dart.

  • Lists: Dart has List to represent ordered collections of data. Lists are mutable.

  • Maps: Dart has Map to represent key-value pairs. Maps are also mutable.

  • Objects: Dart supports object-oriented programming with classes. Objects instantiate classes.

  • Null: Dart has a special null value to represent the absence of a value.

  • Runes: Dart has Runes to represent Unicode characters.

  • Symbols: Dart has Symbols to represent identifiers.

In Dart, variables are typed by default. You specify the type when declaring a variable:

int age = 30;  
double price = 19.99;
String name = 'John';
List friends = ['Mary', 'Peter']; 
Map data = {'name': 'John', 'age': 30};

Dart also supports type inference, so you can omit the type for simple variables:

var age = 30;  // infers int  
var name = 'John'; // infers String

Dart handles data through its type system, null safety checks, collections and object-oriented features. The type system ensures type safety and catches errors at compile time.


Variable Scope

Dart has 3 scopes for variables:

  1. Local scope: Variables declared within a block ({}) are local to that block and can only be accessed within that block.
void main() {
  var name = 'John';  // Local to main()

  {
    var age = 30; // Local to this block
    print(age);  // Accessible 
  }

print(age);  // Not accessible - outside scope
}
  1. Function scope: Variables declared within a function are accessible within that function.
void printName() {
  var name = 'John';  // Local to printName()
  print(name);
}

print(name);  // Not accessible - outside function scope
  1. Global scope: Variables declared outside any function are globally accessible.
var name = 'John'; // Global

void printName() {
  print(name); // Accessible from within function
}

print(name);  // Accessible anywhere

So in summary, Dart variables have:

  • Local scope: Within the block they are declared

  • Function scope: Within the function they are declared

  • Global scope: Accessible anywhere in the file

The scope determines where the variable can be accessed from. Dart uses lexical scoping where the scope is determined at compile time based on the variable declaration.


Data Coercion

Data coercion in Dart refers to implicitly converting a value from one type to another. This allows values of different types to be used together.

Dart performs data coercion in the following situations:

  • When performing operations between values of different types. Dart will coerce the values to a common type to perform the operation:
int x = 10;  
double y = 5.0;

print(x + y); // Coerces y to an int and performs int addition
  • When passing arguments of the wrong type to a function or constructor. Dart will coerce the argument to the expected type:
void printString(String s) {
   print(s); 
}

printString(10); // Coerces 10 to a String
  • When assigning a value of one type to a variable of another type. Dart will coerce the value to the variable's type:
int x;
x = 10.5; // Coerces 10.5 to an int

Dart performs coercion in the following order (from high to low precedence):

  • int -> double

  • double -> int

  • bool -> int (1 for true, 0 for false)

  • String -> int / double

And so on...

So in summary, data coercion allows Dart to:

  • Perform operations between values of different types

  • Pass arguments of the wrong type to functions

  • Assign values of one type to variables of another type

Dart performs implicit coercion when required, converting values to a compatible type. This makes the language more flexible and convenient to use.


Disclaim: This article is created with Rix (AI). At this time Rix is free to use, this may change in the future so I create articles as fast as I can to learn Dart.