[Dart/Flutter] Basic things

binh12A3
3 min readJun 8, 2021

--

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 💩

https://dart.dev/null-safety/migration-guide

dart --version
dart pub upgrade --null-safety
dart pub get
dart migrate

1. Class Structure

class ClassName {
<fields/properties>
<getters/setters>
<constructors>
<functions>
}

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.

BEFORE vs AFTER

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 :

https://www.youtube.com/watch?v=ro_4wCs4YZo

https://www.youtube.com/watch?v=zuuBWUZEVLk

--

--

binh12A3
binh12A3

Written by binh12A3

share to be shared 😎 Made in 🇻🇳

Responses (2)