Dart Object Oriented Programming

By pjain      Published April 25, 2020, 1:31 a.m. in blog Programming   

Part of Dart Series

Dart OOP

Dart OOP 101

  • In Dart, every class IS AN INTERFACE - so there is no separate "interface" keyword.
  • There are no public, private and protected - ? did these get added in later versions?

    class User { String id; String userId; String subject;

    User(this.id, this.userId, this.subject); // constructors are simple - types are as declared in class } class MyFavoriteUser implements User { .. extensions }


    // Compact style is preferred - both are the same
    User(this.id, this.userId, this.subject);  // constructors are simple - types are as declared in class
    User(id, userId, subject) {
            this.id = id;
            this.userId = userId;
            this.subject = subject;

    // You can define optional and default values for arguments in constructors using optional arguments
    User({this.id, this.userId, this.subject="default"});

    // Constructors can have alternative arguments eg create from map of json
    User.fromJson(Map<String, String> json) //argument declaration
              : id = json['id'], userId = json['userId'],  subject = json['subject'];

Instantiate, final vs const

Instantiate - type is inferred no need to repeat, also avoid the new as it is optional... var phil= User("123","Phillip","Developer");

If the variable value does not change, it is preferable to use final instead of var, to allow compiler to check more. final phil= User("123","Phillip","Developer");

  • if you declare a member variable or constructor const, then compiler can initialize it

    class ClassName { // in usages - compiler will construct a constant instance at compile time - like an immutable string! final String someVar; const ClassName({this.someVar}); }

Getters and Setters

  • You don't need to provide functions for variable access as all public members are accessible by class.member, or assignment.

  • You can define virtual members or additional methods using get and set methods

    String get fullname => this.firstname + ' ' + this.lastname; // so phil.fullname is a new accessor

Factory generators and hidden constructors

  • Internal or hidden constructors // Using _ makes the named constructor internal. _internalUser({this.id, this.userId, this.subject="any"});

  • With the factory keyword you can create a specific instance e.g. by specifying an integer id

  • This is powerful eg, you can maintain a pool of instances and return them (creating more as needed, or releasing) factory User(int i) { //here i is used for id, returns an instance based on i - vs having to specify all arguments return User._internalUser(i.toString(), "My user","any"); }


Standard toString, hashcode

@override String toString() =&gt; 'User: $subject';

Mixin Inheritance and Class hierarchies

  • In Dart, every class IS AN INTERFACE - so there is no separate "interface" keyword.

All classes inherit from Object.

Every object is an instance of a class, basically every datatype including primitives like int and String are all objects so you can 3.toString();

Dart classes have mixin-based inheritance ie a class can extend or implements other classes. (TODO: What is difference?)

Method cascades, fluent interfaces - the .. operator

  • You can "inherit" the original object that a function A() was used and fluently pass it onto function B() and C() var t = "123"; t..toString()..toUpperCase()..split('2'); //easier to think .. as "ditto" - inherit base object!


There are no comments yet

Add new comment

Similar posts

List++ Advanced Application Features

Interview Questions in Flutter

NAV: URL Launcher, Deep Links

NAV: Navigation Drawers