Dart Programming Basics you need to know

Hello Flutter lovers. I am going to discuss the basic dart programming things you need to know. In this Dart Tutorial, I discuss class, functions, interfaces Labeling and arrays (Map lists). Let’s learn dart article “Dart Programming Basics you need to know “.

Class in Dart Programming Basics you need to know

Let’s create a simple class. follow the below code.

class People{
   String name;
   int age;
}

void main() {
  People p = People();
  p.age = 20;
  print(p.age);
}

This is the simple way to create a class in dart. as you can see this is very familiar to Java. Also in here we create a People object in the main method. So this is how we create class in Dart programming. Let’s see how constructors are implementing in the class.

class People{
   String name;
   int age;

   People(String _name, int _age){
      this.name = name;
      this.age = age;
   }
}

void main() {
  People p = People();
  p.age = 20;
  print(p.age);
}

try the below code in your editor. Also you can use DartPad.dev as a online editor. DevPad is the best online editor for dart programming language.

class People{
   String name;
   int age;

   People(String name, int age){
      this.name = name;
      this.age = age;

      print(name);
   }
}

void main() {
  People p = People( name : "BuilITMasters" );
}

try the below code also in your editor.

class People{
   String name;
   int age;

   People({
this.name ,
this.age }){
      print("test");
   }
}

void main() {
  People p = People( name: "BuildITMasters" );
  print(p.name);
}

that is how we create and handle the class in dart. try out your way. try to change the constructor and see how it react. you can use an online editor. ( Click here )

Functions in Dart Programming Basics you need to know

Let’s create a simple function to understand what is function in dart tutorials.

void simpleFunction(){
    print("simple")
}

void main(){
    simpleFunction();
}

See, this is easy. If you want to become a dart developer, I mean you want to become a flutter developer, you need to know the basics in dart programming. Let’s see how functions are working with the parameters in dart.

simpleFunction(String fname, String lname){
    print("$fname $lname");
}

void main(){
    simpleFunction("Alex", "Mach");
}

Try to run this code and get the output.

What if we code like this. refer the below code and get an idea about parameters in functions.

simpleFunction(String fname, [String lname]){
    print("$fname $lname");
}

void main(){
    simpleFunction("Alex");
}

and try this one also.

simpleFunction([String fname, String lname = "Mach"]){
    print("$fname $lname");
}

void main(){
    simpleFunction("Alex");
}

what will be the output. you try this code with different types. Try that and get an idea.

simpleFunction(int age, [String fname, String lname]){
    print("$age $fname $lname");
}

void main(){
    simpleFunction(25, fname:"Alex", lname:"Mach");
}

Abstract Classes

Follow the below code. This is the correct and simple way to code an Abstract class.

abstract class Person{
  String name;
  int age;
  
  Person(this.age ,this.name){
    print("Person Created");
  }

} 

class somePerson extends Person{
  

  somePerson (String name, int age):super(age, name);  
   

}


void main() {
   somePerson person = somePerson ("thisun" , 1);
}

We are creating an abstract class called Person. In that class, we create a constructor. Inside the somePerson class we called that Person abstract class. when we consider about the main method we can create an instance of the somePerson class.

Try the below code using your editor.

abstract class Person{
  String name;
  int age;
  
  Person(this.age ,this.name){
    print("Person Created");
  }

} 

class somePerson extends Person{
  
  String dd;
  somePerson (String name, int age, this.dd):super(age, name);  

}


void main() {
   somePerson person= somePerson ("thisun" , 10, "dd");

}

In here, we called another variable that is not in the abstract class.

Try this code. In here we override some methods.

abstract class Person{
  String name;
  int age;
  
  Person(this.age ,this.name){
    print("Person Created");
  }
  
  void about();
  void about2(){
    print("I am a person 2");
  }
} 

class somePerson extends Person{
  
  String dd;
  somePerson (String name, int age, this.dd):super(age, name);  
   
  @override    
  void about(){
    print("I am a simple person");
  }
}


void main() {
   somePerson person= somePerson ("thisun" , 10, "dd");
   person.about();
   person.about2();
}

Array | MapList in Dart Programming

Follow the below code. that is the simple way to create an maplist in flutter dart.

void main(){
    Map user = { "fname":"Osanda" , "lname":"Gamage" , "age": 25 };
    print(user);
}

Let’s get into little bit advanced maplist in dart. Refer the bellow code.

void main(){
    Map<dynamic, dynamic> user = {
       "fname":"Osanda" , 
       "lname":"Gamage" , 
       "age": 25,
       true : false
    };

    print(user[true]);
}

I think you can get an idea about this. In the print method, we are calling “true”. That is the key, same as fname, lname like wise. Follow the below code.

void main(){
    Map<dynamic, dynamic> user = {
       "fname":"Osanda" , 
       "lname":"Gamage" , 

       1 : { "user":"BuildItMasters" , "Type": "Personal Blogger" }
    };

    print(user[1]);
}

In here also we are calling the key, same as fname, lname like wise. You can see an object as an output. What if we called like this inside the print method.

void main(){
    Map<dynamic, dynamic> user = {
       "fname":"Osanda" , 
       "lname":"Gamage" , 

       1 : { "user":"BuildItMasters" , "Type": "Personal Blogger" }
    };

    print(user[1]["Type"]);
}

The output will be “Personal Blogger”.

void main(){
    Map<dynamic, dynamic> user = {
       "fname":"Osanda" , 
       "lname":"Gamage" , 

       1 : { "user":"BuildItMasters" , "Type": "Personal Blogger" }
    };

    print(user[1][0]);
}

I think now you have an idea about handling maplist’s keys and values.

Try this code.

void main(){
     List fruits = [ "Banana", "Apple" ];

     var ft = fruits.map((fruit) => 'Fruit : $fruit').toList();
     for( var d in dd ){
           print(d);
     }
}
void main(){
     List fruits = [ "Banana", "Apple" ];

     fruits.addAll(["Orange"]);
     print([...fruits , ...fruits ])
}

Thank you for reading.