Quantcast
Channel: Stories by Uday Hiwarale on Medium
Viewing all articles
Browse latest Browse all 145

Dart (DartLang) Introduction: Advanced Dart Features

$
0
0

In this article, we will look at some of the advanced features of Dart which you can live without but they empower our Dart program and help us achieve some tough goals.

Immutability

A value is immutable when it can be modified. Do not get confused with variables. If a variable has a value and we assign a different value, then we did not change the value, we just assigned a new value to the variable.

A value is immutable when its internal structure can not be modified. For example, a Person object can have name property. We can assign a new value to person.name and by doing that, we are changing the internal state of the object. This is called object mutation and person is a mutable object.

In certain cases, we need immutable objects. Immutable object, after they defined, can not be changed. In earlier articles, we learned about final and const. Since we know that, by making instance variables of a class final, those properties can not be changed after assigned some initial value.

When working on bigger projects, we need to make sure all team members are following strict guidelines. One of these guidelines could be to make a class immutable. But some idiot could accidentally add a non-final instance variable in a class which could lead to a chain of events.

To make sure a class must be immutable, we can use @immutable annotation from meta package. When this annotation is placed on the class, it forces the class (and any classes inherit from it) to become immutable.

https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/1.immutability.dart

Factory Constructors

A factory function is a function that returns an instance of a class.

Dart provides factory keyword to label a default or named constructor. Then it becomes our responsibility to return an instance from this constructor. A factor constructor is generally used to control the instance creation.

For example, we can cache an instance of the class and return the same instance when a user is trying to create new object.

https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/2.factory-constructors.dart

Assertions

An assertion is a check to verify if a value is what it’s supposed to be.
Using assertion, we can avoid a disaster caused by a bad value and
terminate the program by throwing an error before that value is consumed.

Dart provides assert function which takes a value or expression and an optional error message. If the value or the expression evaluates to false, it throws AssertionError and terminates the program.

Dart does not evaluate assertions in Production mode. If you are using Flutter, assertions are evaluated in Debug mode. If you are using dart CLI, use --enable-asserts flag to enable assertions. For example, dart --enable-asserts <file-name>

If a class constructor takes named arguments, they are optional and their default value is null. If an instance variable must not be null, then we need to assert it.

https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/3.assertions-on-constructor.dart

Const Constructors

Dart empowers class to create compile-time constant objects.

If a class has final fields, these fields are initialized at runtime but later can not be modified. When multiple objects are created from this class, like by passing the same arguments to the constructor, a new instance of the class is created every single time.

We can create a constant constructor by placing const keyword before it.
Using constant constructors, a compile-time constant object is created

Const constructors are used to create canonicalized instances, which means a class instantiation with the same arguments will return the old instance from the memory. This makes our program memory efficient.

Using the same principle, we are able to create compile-time constant List, Set and Map. These classes also uses the const constructors.
https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/4.const-constructor.dart

Enumerations

An enumeration or enum is an object of type enum that holds constant values. An enum object can be related to a class with compile-time constant static properties. The value of these properties is initialized by Dart at compile time.

Each value in an enum has an index and can be accessed using index property on the value. To get all entries of an enum, we use values property on the enum object.

Enumerations are used widely to create a collection of constant values.
But since the value in an enum does not represent a definite Data Type,
their use is limited to comparisons only and not for the data storage.

https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/5.enums.dart

Spread operator

A spread operator … (three-dots) when placed before a List or a Set or an Iterable, laid out individual elements of that collection.

Spread operator is relatively new to Dart and it is supported in v2.3 and beyond.
https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/6.spread-operator.dart

@required Annotation

By default, all named arguments of a function are optional
and their default values, if not provided, are null.

Using @required annotation from the meta package, named parameters can be marked required. We just need to place it before the parameter declaration.

This also works for a shorthand constructor syntax.

https://github.com/thatisuday/dart-getting-started-guide/blob/master/K.advanced-topics/7.required-annotation.dart

🏠 INDEX PAGE


Viewing all articles
Browse latest Browse all 145

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>