# Simple And Bug-Free Code With Dart Operators

There’s no easy if-else or ternary solution.

Also, in a perfect system, this error won’t exist at all, but we don’t always have perfect systems and APIs.

For this, the ?.

operator comes handy.

The operator roughly translates to “If the object is not null, access inner field, else return null.

”Hence, even our earlier point.

x example would throw a null instead of an error.

This makes our job much easier and we can simply handle a null error by using our last operator.

Here, if point is uninitialised, instead of throwing an error, it returns a null.

This null is then caught by our null check operator which assigns a value 0 to the variable x.

Similarly, we can fix our earlier example like:pages[0]?.

contributors[0]?.

authorDetails?.

basicInfo?.

firstName ??."N/A";Now, if any part is null, it will return a value of “N/A”.

4.

??=??= simply means “If left hand side is null, carry out assignment”.

This will only assign a value if the variable is null.

Unnecessary reassignments of values to variables can be avoided.

5.

=>=> is known as the fat arrow notation in Dart.

It can be used in two different ways.

Both have to do with defining functions.

The first way it can be used is as a shorthand for returning something.

So => x simply means {return x;}However, the operator also works for single statement even without returning anything.

Note that if the return type is void, even => s will not return anything.

6.

.

(Cascade notation)The cascade notation is a simple way to change properties of an object, usually while creating it, rather than getting a reference to the object and changing properties one by one.

The first example we’ll take is our Point class again.

Imagine a situation where we first need to initialise the class and then set the x and y properties.

This is what you probably imagined:Point p = Point();p.

x = 3;p.

y = 6;The cascade notation gives us an easier way to do it without using the object again to set properties.

Note that if we had just used the .

operator, it would not return a Point object on the first line.

What the cascade notation essentially does is:Create the Point object to default valuesChange any affected values by the cascade operationsReturn the original object (Here, the instantiated Point object)This is very useful for when a lot of properties need to be set, in the Builder pattern for example.

The best example for this comes out of the Dart documentation:If you want to explore cascades further, this is a very good resource.

Note that the article is VERY old, but it gets the explanation done.

7.

~/Dart also has a few operators for speeding up arithmetic operations.

The ~/ operator divides and returns the floored (integer part) of the result.

These were a few operators which simplify and keep your code bug resistant.

8.

Bonus: (Planned, not released) Spread operator: …There is talk of adding a … operator to Dart as a spread operator for collections.

Lists can be easily unpacked with this operator.

This will add elements of demoList directly to the list instead of adding it as a List object.

Feel free to check out my other profiles and articles as welldeven98 – OverviewGoogle Certified Android Developer.

Speaker and Blogger for Flutter and Android.

Loves Coding, AI and Chess.

– deven98github.

comDeven Joshi (@DevenJoshi7) | TwitterThe latest Tweets from Deven Joshi (@DevenJoshi7).