O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a factory constructor

Dart gives us many flexible and succinct ways to build objects through constructors:

  • With optional arguments, such as in the Person constructor where salary is optional:
    class Person{
      String name;
      num salary
      Person(this.name, {this.salary});
    } 
  • With named constructors (a bonus for readable self-documenting code), for example, where a BankAccount for the same owner as acc is created:
    BankAccount.sameOwner(BankAccount acc): owner = acc.owner;
  • With const constructors, as shown in the following code:
    class ImmutableSquare {
    finalnum length;
    static finalImmutableSquare ONE = const ImmutableSquare(1);
    constImmutableSquare(this.length);
    }

However, modern modular software applications require more flexible ways to build and return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required