• DART LANGUAGE FEATURES

    Discussing some important and interesting Dart Language features below:


    Variable Declaration and Initialization

    • Variables are declared in Dart using the variable data type and the variable name. e.g.:  String firstName; 
    • Variable initialization consists of assigning valid values to a declared variable. e.g.:  String firstName = "John"; 

    String interpolation

    • Remember, Strings can be declared in Dart using either single or double quotes. 
    • String interpolation is a way to substitute a String variable's value within a string literal. e.g.:  
                      String name = "hello";  
                      print("Greetings: $name");  
    • String interpolation can contain more complex expression and in which case we use curly braces {} to evaluate the expression. e.g.:  
                     String name = "hello";  
                     print("Greetings: ${name.length} letters");  

    Type inference with var keyword

    • Dart can use type inference to determine a variable-type in cases where values are assigned to that variable. This way we don't have to explicitly declare a type for that variable. 
    • Now this doesn't mean you can change the type of such type inferenced variable after it has been initialized. This is where Dart's static typing comes into play and prevents you from changing the variable's type. For example this is illegal in Dart: 
                    var myAge = 25; 
                   // In following line, Dart throws compile time error since myAge when first declared was an integer 
                  myAge = 'twenty-five';  

    var and final keywords in Dart

    • A variable declared with var keyword is mutable and hence its value can be  changed to another value of the same  type. 
    • Whereas a variable declared with final keyword is immutable and hence its value cannot be modified once its value is set. It is preferable to use final variables wherever possible since they provide modification-safety on top of built-in type-safety of Dart. 

    The dynamic keyword

    • Using this keyword allows a variable to be of any type i.e. you can change the type of a variable from one to another. 
    • Although, Dart provides this flexibility, it is recommended to use either var or actual data-type whenever possible. 
    • Only use a dynamic type when you truly don't know or aren't sure ahead of time what the type of a variable would be.  

    Optional Parameters in a Dart Function

    • A function in Dart can have zero or more parameters (also called arguments here). 
    • If a function has one or more parameters, we can declared a parameter to be optional by wrapping that around square brackets. This will allow the caller to omit that optional parameter if the caller chooses to do so. 
    • A good practice is to assign default values to such optional parameters so avoid null values. A default value can be assigned much like any other variable initialization. E.g.:  
                   String getDescription(int id, [String name = "John Doe"]) { 
                       return "some description based on id"; 
                 } 

    Named parameters in a Dart Function

    • Dart functions allow parameters to be passed to them in two ways  
      • positional - in which the position of a value in the caller must match the position of the corresponding parameter or else there would be syntactic or semantic errors. 
      • named - in which the function gives a name to each parameter so that the caller has to specify the parameter name when calling the function. e.g. of this is: 
                String getDescription({int id, String name = "John Doe"}) { 
                    return "some description based on id"; 
                } 
    • The caller would look like : 
                     getDescription(id: 100, name: "Alex Jones"); 
      

    Mixins in Dart

    • Mixin is a way to extend the functionality of existing Dart Class. 
    • mixin is created with the keyword mixin. 
    • Mixins allows us to extract common code and reuse code in various classes.
    • A class that uses a mixin using a with keyword cannot define the same variables and method names as that of the mixin. Following is a detailed example of building and using a mixin: 

    Generics in Dart

    • Generics allow us to add types to generic data constructs like List, Map etc 
    • Setting a type like List<int> prevents the code from assigning or adding types other than int type to this list thus improving type safety and maintainability. 
    • Note though that using type annotation isn’t compulsory in Dart, however it is considered a good practice to do so.

    Closures and the fold method

    • The fold method can be used in any collection that implements Iterable like a List. This method reduces a given collection to a single value by iteratively combining every element within the collection within an existing value. 
    • A closure in Dart can be thought of functions without names. These functions can be passed as parameter (also called callback in languages like JavaScript) to other functions. A fold method also expects a “combiner” function to reduce a given collection to a single, final value. 
    • Here is an example showing addition of list of numbers using a for loop and a fold function: 

    Enums in Dart

    • Can be used to represent finite set of values like colors of a rainbow. These values then act like constants which we can use in our code. 
    • Note that (at the time of this writing), Dart doesn’t support values for individual enums.

    Default, required and assert parameters in Dart and Flutter

    assert statement: An assert statement is used to stop the normal execution of your program if the boolean condition it represents is true. Asserts are removed during production process which makes sense since asserts are there to help during development. Asserts can be used in conjunction with @required annotation and parameter default values to implement effective dart code. 

    @required assertion: is a feature from Flutter framework which specified that the caller of a widget doesn't skip this required parameter. This assertion doesn't, however, stop someone from assigning null or bad values to that parameter. 

    A default parameter value in a function or constructor of a Dart class assigns a default value to a parameter. Such default value is used in cases where the parameter is skipped (i.e. not mentioned) in the caller's parameter list.
  • 0 comments:

    Post a Comment

    ADDRESS

    Lalitpur, Kathmandu Valley, Province 3, Nepal

    EMAIL

    ashish.karki3@gmail.com

    TELEPHONE

    +977-9861487950