Variables in Dart

We can use variables in Dart to store data in memory, in order to use the data multiple times during the code along the way.

The naming convention for variables is the same as functions, we use a camel case convention that starts with a lower case.

we can define the type of a variable or use the keyword var.

 

if we are assigning the variable to a value then we should use the keyword var, although this is not essential.

var name = "Mostafa"

 

but if we were instantiating the variable then we would use the appropriate type for the variable:

double numOfbitcoin;

numOfCars = 2.3;

 

void main(){

   var name = "Mostafa"

   double numOfbitcoin;

   numOfCars = 2.3;

}

 

 

why do we need classes in Dart?

In Dart, everything is an object, similar to the real world where for example you have a car and the car can drive or it has 4 wheels and 7 seats.

 

we can group data together in dart by creating objects, objects are classes and their name always starts with an upper case.

 

let's see an example without classes, let's say I had the variables name and age and both of these variables belonged to a person of interest, I could do something like:

void main(){

   var name= "Mosatafa"

   var age = 25

}

 

but I know both name and age belong to the same group and it would make sense to put them in an object, so let's create an object blueprint and put these variables in there. 

 

as we mentioned above, if we are not instantiating the variable to a value at the time of creation, it's good to put the appropriate type, so i'm going to do that below.

class Person{

     String name;

     int age;

     name = "Mostafa";

    age = 25;

}

 

void main(){

   var p1= Person();

   print(p1.name);

   print(p1.age);

}

 

 

The code above works but it's not dynamic, we want our classes to be able to take different values whenever they are called depending on the situation.

To achieve this we can use constructors which is a special function inside a class (method) that gets called every time you create a class.

 

 

in dart to create a constructor we just use the same name as the class name for the method as shown below:

 

class Person{

     String name;

     int age;

     Person(String inputName, int inputAge){

                name = inputName;

                age = inputAge;

       }

}

 

and now we could call the class using different values:

Person('Mostafa', 25);

Person('John',30);

 

to add named variables to our class we put the Constructors parameters inside curly brackets {} and to pass on a default value we just use the equal sign.

in Flutter, not dart, you can use the @required before the parameters to make the class compulsory to receive a value when it's created

class Person{

     String name;

     int age;

     Person({@required String inputName = 'Mostafa',@required int inputAge = 30}){

                name = inputName;

                age = inputAge;

       }

}

 

of course, constructors are very common in Flutter and dart so there is a shortcut. you can use the "this" keyword to access class variables inside the constructor and the constructor will assigned the passed on value to the variables, there is no need to run any code inside the function.

 

 class Person{

     String name;

     int age;

     Person({@required this.name = 'Mostafa',@required this.age = 30})

}

 

 

Note** 

Classes can have multiple constructors too, 

 

let's take Person for an example again, the normal way of calling the constructor is by just calling the class name inside a class.

You can also call the class name.anyName(){} this will enable you to call your class with different configurations, and you determine how to deal with different configuration inside your constructor and class

class Person{

  String name;

  int age;

  Person(this.name, this.age);

  Person.Developer(this.name){

    this.age = 90;

  }

}

  

void main() {

  var p1 = Person('James', 30);

  var p2 = Person.Developer('max');

  print(p1.name);

  print(p2.age);

 

}