1.2 Variables and Operators
Variables
var Property
When you want to initialize a variable, you can use var, this kind of initialization allows us set any type of data to this variable. Example:
var name = 'Alan',
var age = 31;
You cannot do this:
var name = 'Alan';
name = 21; // This is wrong
Once you create a variable with any type, this variable will keep that type of data in the whole program
Pre-defined variables
There is a way to define specificlly what type of data you want setting to each variable in the code.
int age = 31;
String name = "Alan"
Numeric
int: Integersdouble: Numbers with decimalsnum: They could be ethier integers or decimals
int n1 = 1;
double n2 = 1.5;
num n3 = 1;
num n4 = 1.5;
A double can be limited in decimals using toStringAsFixed() function.
double number1 = 22.998;
String numerWithTwoDecimals = (number1).toStringAsFixed(2);
// Result must be 22.99
String
String name = "Tux";
String lastname = "edo";
Concat:
String fullname = name + " " + lastname;
or
String fullname = "$name $lastname";
Booleans
bool light = false;
light = true;
Dynamic types
These are variables that could changes its value and type through the program excecution
dynamic example = "Hi, Im a example";
example = 23;
Fixed data types
They are fixed and cannot change its value through the program
final String example1 = "Alan"; // In compiling execution
const String example2 = "Alan2"; // In running execution
Both are unable to modify but one is using while the programm is running (const), by other way, final is a value defined in compilation time.
Convertions
We can convert from String to Integer or vice versa
String stringToNumber = "32";
int numberOk = int.parse(stringToNumber);
int numberToString = 32;
String stringOk = numberToString.toString();
String stringToDouble = "34.2";
double doubleOk = double.parse(stringToDouble);
Operators
~/: Divison without decimals/: Division*: Multiplication+: Sum-: Rest%: Module