Things you need to know about Dart language

CodingWithTashi
4 min readJul 6, 2022

Sometimes you might want to learn a new technology/framework based on your interest or your organization's requirement and if you happened to be an experienced developer It is quite easy to shift between technology. But there will always be some differences.

Dart

Hello guys, this is CodingWithTashi and in this shitty post, we are going to discuss some of the things I have learned in flutter after coming from a native java developer.

Note:

  • I have been working on native android for years building lots of crappy apps and then when I started flutter/dart, it was a smooth experience since both dart and java share a lot of things in common. But that is not what we are going to discuss today.
  • In this post, I am going to share some of the things that I have noticed while working with flutter/dart.
  • I will be comparing dart with java throughout this post and you are welcome to compare it with any other language that you know and see if it is the same in dart or not.

Let’s get started with the list

  1. Naming convention

Unlike Java, Dart can have a different class name and file name, and file names are standard to use lower camel case. eg.

file_manager.dart
test.dart

2. Object References

Can you guess what will be the output of the below code?

void main() {
Person bob =Person("Bob");
print(bob == Person("Bob")); //print false
}
class Person{
final String name;
Person(this.name);
}

Well, the answer is false even though we know both objects have the same value that is because by default, when you compare objects in the dart, Dart doesn’t compare objects by their value.

Solution:

You can use equitable package to resolve this issue.

import 'package:equatable/equatable.dart';void main() {
Person bob =Person("Bob");
print(bob == Person("Bob")); //print true
}
class Person extends Equatable {
final String name;
Person(this.name);

@override
List<Object?> get props => [name];
}

3. Cloning object.

Can you guess what will be the output of the below code?

void main() {
Test t1 =Test(name: "test1");
Test t2 =t1;
t2.name="test2";
print(t1.name); //print test2
print(t2.name); //print test2
}
class Test{
String name;
Test({required this.name});
}

The answer is that both will print the same value since t1 and t2 reference value is the same. whenever you update t2, t1 will have the same data.

Solution:

Add copyWith method to clone object

void main() {
Test t1 =Test(name: "test1");
Test t2 =t1.copyWith(name: t1.name);
t2.name="test2";
print(t1.name); //print test1
print(t2.name); //print test2
}
class Test{
String name;

Test({required this.name});

Test copyWith({
required String name,
}) {
return Test(
name: name,
);
}
}

4. Overloading doesn’t support in Dart

Unlike java, Dart doesn’t support method overloading.

Below code will throw compile time error.

class Test{

void printName(){

//logic
}
void printName(String name){
//logic
}
}

Solution:

Use name parameter

class Test{

void printName({String? name}){

if(name==null){
//first logic
}else{
//second logic
}
}
}

5. No interface in dart

Dart doesn’t support an interface, unlike java dart doesn’t have an interface keyword

Below code will throw compile time error

interface Test{

void printName();
}

Solution:

You can use an abstract class.

abstract class Test{

void printName();
}

6. Communication between Isolates/Thread

You can’t communicate between isolate/thread directly. You will need to communicate via SendPort and Receive Port in dart. For more, you can read here.

7. Access Modifier in dart

In Java, we can use public, protected, and private keywords to control the access scope for a property or method.

However, Dart doesn’t provide that kind of keyword. Instead, you can use _ (underscore) at the start of the name to make a data member of a class becomes private.

So, a data member is either public (if not preceded by _) or private (if preceded by _)

Example below.

class Test{

//public method
void printName(){

}
//private method
void _getApiKey(){

}
}

8. Method without class

In Dart, you can have methods without class.

As you can see here, there is no class defined in the given code and still, the application will run without compile error.

void main() {
outSideMethod();
}
void outSideMethod(){
print("This is mthod without class");
}

9. Mixin

Since dart doesn’t support multiple inheritances. Dart uses a concept called mixin. We use with keyword followed by one or more mixins names.

class Area {
// some area code
}
class Perimeter {
// some perimeter code
}
class Box extends Perimeter with Area {
//common code
}
void main() {
// you can now access both Area and Perimeter
var p = Box();
}

10. Setter and getter

Unlike java, Dart has get and set as keywords for getter and setter.

You can use get and set keywords directly in the model class.

class Test {
String _name;
Test({
required String name,
}) : _name = name;

String get name => _name;

set name(String value) {
_name = value;
}
}

That’s all for now, Thanks guys, I hope you like this shitty post. please make sure to give a clap 👏 and leave some engagement.

Check out more similar posts from CodingWithTashi. Connect me on Twitter, Linkedin

--

--