[Dart/Flutter] Basic things

First of all, it’s recommended to migrate to a new feature of flutter called “null-safety”. Because sooner or later we have to face it 💩


1. Class Structure

1.1 Properties

Properties : are variables used in classs (i.e : name, age, color)

1.2 Constructors

Constructors : are prototypes with input parameters when creating a new instance of class (i.e: Dog())

If we don’t create any constructor, then dart itself will create an empty construcor “Dog()”. And we absolutely don’t want this. We expect when creating a new dog, we’ll provide info like : name, age, color,… SO, we need to create our constuctor.

Parameter constructor

1.3 Functions

1.4 Setters và Getters

As you can see the “name” is a public variable (property), so it could be updated unexpectedly. To resolve that, we need to make it as private variable by adding “_” (i.e : _name) together with its getter/setter.


Note : if both classes are in the same dart file. Then adding “_” to make private won’t work as expected.

2. Class features

2.1 Extends (tính kế thừa)

Variable “ _leg” and method barking() are inherited from class Animal

2.2 Mixed (tính đa kế thừa)

2.3. Abstract (tính đa hình)

Class extends/implements from abstract-class have to override its methods

3. (Optional) Positional/Named parameters

In “Positional parameters” we can use “[]” for optional parameters.

Due to null-safety feature, these optional parameters must have default values or declared as nullable (i.e : int?)

4. Keywords

4.2.1 Static

You can use “static” keyword to access directly into class, no need to create new object/instance of class.

NOTE : The static variables can’t be accessed via object, but class level.

4.2.1 factory + Named constructors

Using named constructor, you can create multiple constructors. And with “factory” keyword, you can return multiple type of constrcutors.

🤓____________TO BE UPDATED____________🤓

References :



“Developer never die” 😎🇻🇳