Quick learning Dart (Flutter) with the Kotlin language (Part 4)

Tram Ho

1. Introduction

This series is written to help those who know the Kotlin or Java language quickly learn the Dart language to code Flutter. If you have not read the previous section, you can read them again here

2. Class

The class in Dart is quite similar to Java. The class naming convention is the same as Kotlin. Below I list the basic components of a class including property (Dart called data), constructors, member funtions (also known as methods), static variables and static functions.

  • If you want to create a variable or a static function, add the static keyword first
  • Line User(this.id, this.name, this.age); code User(this.id, this.name, this.age); in Dart it is the equivalent of this code in Kotlin

  • If in Class you do not declare any constructor, there will be an empty constructor (no param) by default created in your class and once you have created a constructor with param then this empty constructor. will disappear. That is with the above code that calls var user = User(); error because constructor cannot be found. This looks exactly like Kotlin to Java.

3. Data class

Also use the above code. If we try to print the user object: print(user); We will get an output Instance of 'User' , not User(id=1, name=Minh, age=18) as we often see in Kotlin. This is because:

Dart doesn’t have support for data class like Kotlin does.

Bitter (facepalm). Without supporting the data class, you must overide the toString function manually.

Now the output looks like Kotlin:

4. Named constructors

Dart allows us to name constructors to create more than one constructor. I try to create 3 constructors:

Output:

Since the other name variables have no initialization value, it is null . Easy to understand

5. Redirecting constructors

This term at Dart is similar to Secondary constructors in Kotlin. It helps us to create a constructor from another constructor in the same class.

6. Import

Import the files I coded

Dart treats each .dart file as a separate lib . That means even if the 2 .dart files are in one package, you must use import . This is different from Kotlin ha.

Now try to create 2 .dart files. In the user.dart file, I create a class called User . In the remaining .dart file, I will create the main() function and initialize the User object. As a result, Dart it requires import 'user.dart';

Try to move the user.dart file to a folder called example and see the results. Now the directory path has changed to import 'example/user.dart';

Import Dart’s core library

Import Dart’s core library. Syntax import 'dart:xxx' . Math library example: import 'dart:math'

Import lib from dependeny

Syntax: import 'package:<tên package>/tên file dart' . For example:

import as

Suppose we have 2 libs, lib1 and lib2. Both libs have Element class, we will distinguish by using import ... as ... :

Import part of the lib

If we just want to get part of lib, we don’t want to get all. Can use show/hide

7. Access modifier

In Dart, there is no access modifier (public, private, protected, internal) like Kotlin or Java. Default is all public. If you want to make a variable, function, or constructor private, add the underscore _ character before the name.

Try creating a new user.dart file and then creating a class in this file

Now go to the main.dart file to code the main function:

Can you see it error? So I can’t call the _age variable and the _printName() function because it’s private

8. Setter, getter

If there is a private variable, it is necessary to have a setter, getter function, right?  Dart gives us two keywords set and get to do this.

9. Factory constructor, Singleton

Dart provides us the factory keyword to create a special constructor, when using this constructor to create the object it won’t create a new object if it sees an existing one already. In short, it works like Singleton Pattern. So I will try using factory to create a Singleton class.

Now try to test if 2 objects have the same address or not?

10. Cascade notation

This is quite similar to apply in Kotlin. Syntax: ..

Do you see Dart error in the print command ?. That is why I say it’s quite similar but not exactly like: v. .. in Dart it only calls the instance method / property and the print function print it. So just adjust like this is:

11. Enum

Enum in Dart has a lowercase letter, while Kotlin has it in uppercase.

12. Abstract class

Abstract class it is just like normal class, also has constructors, methods and properties. The only difference is that it has an additional abstract method (method without body). For example:

13. Inheritance

Dart doesn’t support multiple inheritance, meaning a class can only have a maximum of one parent class. We use the extend keyword to extend the parent class. A parent class can be an abstract class can also be an ordinary class. When extend abstract class, we are required to override abstract methods (methods without body).

Dart does not distinguish Interface from Class like Kotlin, but it considers each class to be an interface. Thus, a class can inherit another class with the keyword implement . A class can only extend one class but can implement many other class . In Dart there is no concept of final class like Kotlin, which means that all classes can be inherited.

Because Interface in Kotlin is a non-friendly method, when override it will not be duplicate code. And Dart, the code in the Dog class is duplicated code with the class it implements as Walkable , Flyable as in the picture I have notes. If you just want to reuse the code of the Walkable , Flyable class without Walkable a function exactly the same (duplicate), then you use with instead of implements .

with

Now I will example the code that uses both extend , implements and with :

Using with you will not be forced to override all functions, properties, which means that you like it, you can also override it or else it will reuse the code. Also, using implements is forced to override it all, which means that there will be cases of duplicate code like my example above.

This with in Dart is called Mixins . This term may be quite unfamiliar to you, so I would like to quote the text from the doc as:

Mixins are a way of reusing a class’s code in multiple class hierarchies.

Literally is a place to provide methods and properties for other classes to use without having to parent them.

mixin

Dart gives us an additional keyword mixin . In the Inheritance example above, you try to replace the class Walkable with the mixin Walkable and the result will still be the same. mixin differ from regular class in that:

  • mixin has no constructor, so it cannot create objects.
  • mixin can only be used to implements or with but not extends
  • mixin can limit which classes are allowed to use their code with the keyword mixin on

mixin on

As I said above, we use mixins on to limit which classes are allowed to use our code.

Looking at the photo above can easily understand that:

  • mixin XOnA only for those classes that are children of class A namely class P , likewise with the mixin YOnB .
  • class T not allowed to use mixin YOnB because it is not a child of class B
  • class R not allowed to use mixin XOnA because it is not a child of class A Exactly, class R is a child of class Object so editing this will be ok:

14. Override operator

In Kotlin, each class allows us to override equal function then Dart overide operator ==

In addition to the == operator, you can also override the other operators in the following table if you want

Conclude

By now, you should be able to code Dart. Hope you guys continue to watch the sequels

Reference: https://dart.dev/guides

https://medium.com/flutter-community/https-medium-com-shubhamhackzz-dart-for-flutter-mixins-in-dart-f8bb10a3d341

Share the news now

Source : Viblo