Skip to content

Latest commit

 

History

History
91 lines (62 loc) · 3.27 KB

4. Dart Basics.md

File metadata and controls

91 lines (62 loc) · 3.27 KB

--> Dart Functions

    void main(){
        print('hello')
    }

-- void is type here which tell that this function returns nothing.

-- main is the name of this function, generally we can give any name to our function but in this case main keyword is dart special function which is entry point of our dart application. dart automatically calls the main function for you when the app starts.

--> Dart Types

-- dart is strongly and strictly typed langauage. therefore we should use types for arguments we passing on function or what function is return type for that too.

-- dart has dynamic type too which adapt based on the received arguments but we should strongly avoid that too.

-- string :- 'hello', '2', "Ash" -- integers :- 10, -2 -- float :- 10.5, -10.56

    #Example

    void addNumbers(int num1,int num2){
        print(num1 + num2);
    }

-- in above example we can see that we add int keyword before the argument which tells it that argument we recieve can only be integer. otherwise if we pass string we will get compilation error. -- also this functions return nothing so we added void keyword before the function name.

    #Example

    int addNumbers(int num1,int num2){
        return num1+num2;
    }

-- we can also return something from the function if we use return keyword but then we also have to set the return type of the function. we cant use void there.

--> Idea Of Variables

     var firstName = 'Ashish'
        firstName = 'Kreideprinz'
     String lastName = 'Albedo'

-- variable firstName holds the data for us in memory so we can use it later somewhere else.

-- to create a variable we have to use var keyword following along with the variable name(firstName) then equal operator to store some data('Ashish') in it.

-- we only use var keyword once when we create it but later if we introduce it or change its value we only call it by the variable name.

    double num1 = 5;

-- we can also create variable beside using var using the type of data it holds.

-- dart has a feature called type Inference which can infer the type of data for variable which you will store here by the value you have on the right side. so its better to use var keyword to create variables.

    // var someNumbers;
    double someNumbers;

-- in this case you can see that variable is uninitialized which means some points later we want to assign a value to this variable so it is considered a good practice that we use the type of data which we will store later instead of var.

--> Object-Oriented Programming

-- Dart is Object-Oriented Programming Language which means everything in dart is object. -- Everything in dart is basically a data structure with a lot of different information in it.

    Class Person{
        String name = 'Ash';
        int age = 22;
    }

    void main(){
        var p1 = Person()
    }

-- You can create objects using Class. A Class allows u to define blueprint for objects And Class Has a Name which start with first letter of it in capital-case. they can also holds variables or properties and also default value to it.

-- We can call the Class using Person() . this will instantiate this call and store in variable p1. we can access the property using . dot notation (p1.name).