Summary

In this chapter, you learned that a constructor is a method that is invoked to create an object from a class. There are generative and factory constructors. The main purpose of a generative constructor is to initialize the instance of a class in a safe manner. The constructor can have required or optional parameters. The optional parameters enable the supply of arguments for only a few parameters from the list of optional parameters.

A constructor can be named. The named constructors provide intuitive and safer construction operations, because named constructors have similar code and some of them can be translated into redirecting constructors. A redirecting constructor calls another constructor that makes the code compact.

Dart supports private constructors. If a class has only private constructors, Dart cannot create an instance of a class. Private constructors are usually used in classes that contain static members, which are only useful in combination with the factory constructor. A factory constructor usually implements the popular factory method and singleton design patterns.

No language can exist without constant variables or constants. Constants play a significant role in programming with Dart and constant constructors can create compile-time constants as instances of a class.

The initialization of variables can happen in several places. Some variables must be initialized during declaration; others can be initialized via constructor parameters. Final variables must be initialized during the instantiation of class. This can happen during a declaration, via constructor parameters or in the constructor initializer list.

This syntax in Dart very often provides a form of syntactic sugar. One of those places is a method call that allows you to execute a class instance as a function. Another one is the cascade method invocation that makes the code less verbose.

In the next chapter, we will discuss advanced technologies to organize asynchronous code execution and learn the best practices to use Futures, Zones, and Isolates in different cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.59.198