Dart Operators — Dart Programming — Part 4

Hello guys. So I’ve written 3 stories about dart basic. We’re going half way to learn basic about dart languages and i think it’ve enough preparation before you can take a step into Flutter World. So let me help you till the end. And, FlutterWithMe!

Today topic is about Operators. In dart we’ve many operators so i just take a quick look through there common.

Arithmetic operators

There are math operator that help you calculate for number type in Dart.

int a = 8;
int b = 3;
int sumAB() => a + b; // Result is 11
int subAB() => a - b; // Result is 5
int mulAB() => a * b; // Result is 24
double divAB() => a / b; // Result is 2.666
int divIntAB() => a ~/ b; // Result is 2
int modAB() => a % b; // Result is 2

And another operator is prefix and postfix increament and decreament operator.

Increament and decreament operator
int a;
int b;
a = 0;
b = ++a; // Increment a before b gets its value.
assert(a == b); // 1 == 1

a = 0;
b = a++; // Increment a AFTER b gets its value.
assert(a != b); // 1 != 0

a = 0;
b = --a; // Decrement a before b gets its value.
assert(a == b); // -1 == -1

a = 0;
b = a--; // Decrement a AFTER b gets its value.
assert(a != b); // -1 != 0

Next one is Equality and relational operators

Equality and relational operators

These operator can apply all for number but for string you only can use “== and !=” operator. If you want to compare two object, dart have function identical(). But with me, i prefer using Equatable for easier comparasion two object (which i’ll tell in another topic).

Type test operators

These operators are handly for checking types at runtime. “is and is!” return bool value for checking is correct type or not.

var name = "Hoang";bool checkIsString() => name is String; //result is true(name as int).toDouble(); //This'll cause error

But “as” is difference. It’ll force your variable to excatly type you want. And maybe cause error if it not this type. So be careful when using this one.

If you not sure about variable type, can check type like this.

var name = "Hoang";if (name is String) {
// Type check
name = 'Bob';
}
else if(name is int){
print("Not int type");
}
Logical operators

These operators help you combine many comparasion condition and priority which one want to pass first.

// This function need both isSupported == true and platform == "Android"if(isSupported && platform == "Android"){ 
print("Android platform supported");
}
// This function need isSupported == true or platform == "Android"if(isSupported || platform == "Android"){
print("Supported or Android");
}
// This function need both isSupported == true and platform == "Android" or "IOS"if(isSupported && ( platform == "Android" || platform == "IOS")){
print("Supported and android or ios platform");
}

Next one is what i favorites Conditional expressions. If you tired with write if else block, Dart have “? :” operator.

double paddingLeft = 0;if(Platform.isAndroid){
paddingLeft = 120;
}else {
paddingLeft = 60;
}
// is similar with this one
paddingLeft = Platform.isAndroid ? 120 : 60;

Easy and perfect, you’re reduce code line to only one line. Other one is null-aware value.

String playerName(String? name){
if(name != null){
return name;
} else {
return "Guest";
}
// is similar with this one
String playerName(String? name) => name ?? 'Guest';

Finally, let go to cascade operator and spread operator

I’ll take basic example about cascade operator and you’ll understand why i love it. It’s same line code but you’ll save effort when write this one, so try to use it into your code.

var address = getAddress();
address.setStreet(“Elm”, “13a”);
address.city = “Carthage”;
address.state = “Eurasia”
address.zip(66666, extended: 6666);
// is similar with this one
getAddress()
..setStreet(“Elm”, “13a”)
..city = “Carthage”
..state = “Eurasia”
..zip(66666, extended: 6666);

Spread operator that help you insert another list into list. It’ll help you when code with Flutter in Row or Column (which i’ll talk in Flutter series).

/ main function startvoid main() {List list1 = ["Geeks","For","Geeks"];List list2=["Wow",...list1,"is","amazing"];print(list2);}

I think it’s long enough for Operators. Hope i help you have an overview about Dart enough. Thanks for reading!

--

--

--

I've started with Flutter since 2019 and have good knowledge about Flutter and some architecture. I want to share more of my knowledge and learn more from other

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web decoration and web design

Few RestAssured (Java) advanced tips for the intermediate skilled

Getting started with Python.

Linked List Data Structure

Download Bettas Mod 1.16.4 for Minecraft — Adds a new type of fish

Download Bettas Mod 1.16.4 for Minecraft — Adds a new type of fish

Osmosis Support Lab — Spring 2022 Recap

How to Build a Cloud Version of Your Open Source Software: Part 1

[Notice] KAS SDK (caver-js-ext-kas, caver-java-ext-kas) v1.1.0 Released

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hoang Nguyen

Hoang Nguyen

I've started with Flutter since 2019 and have good knowledge about Flutter and some architecture. I want to share more of my knowledge and learn more from other

More from Medium

A tour with Dart syntax — Dart programming — Part 2

Custom Option Selection Widget — Flutter.

A Simple Bus Schedule Viewing App using Flutter

Bus Schedule View Widget

Definitive Guide to Unit, Widget and Integration Testing Flutter Apps!