https://smazee.com/uploads/blog/Dart-Programming---Tips-and-Tricks.png https://smazee.com/blog/dart-programming-tips-and-tricks

Dart Programming - Tips and Tricks

To code faster and efficiently one must also learn the shortcuts of that programming language. Reduce your time of coding and make your Flutter app faster by knowing some of the tricks. Here we go….

1. Null-aware operator

Null-aware operators allow you to make operation based on whether the variable value is null or not.

Instead of writing:

if (foo == null) {
 foo = ‘Hello!’;
}

Just do:

foo ??= ‘Hello!’;

2. Double Dot (Cascade notation):

Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.

Instead of doing:

o = new Address();
o.set(country);
o.set(zip);

Now we can:

o = new Address()
 ..set(country)
 ..set(zip);

This will be great to organize your code in easy way.

3.Spread operator (…) Triple dot

You can use the spread operator (...) to insert all the elements of a list into another list:

a=[1,2,3];
b= […a,4,5];
print(b); // [1,2,3,4,5]

This operator can be even used with Sets. You can also conditionally implement using if function.

b= [if(true) …a ,4,5];

If the expression to the right of the spread operator might be null, you can avoid exceptions by using a null-aware spread operator (...?):

4. Using interpolation instead of toString.

You need not to use toString function often.

 x.toString()

can usually be replaced by

 ‘$x’ or ‘${x}’

5. Function call in map

Call a function with a parameter with return function.

Instead of

xs.map((x) => foo(x))

do

xs.map(foo)

6. Null-check Operator:

?? is a null check operator. It can be said as a simplified version of conditional operator to check if variable null or not.

String name = person.name ?? “Pravin Kumar”;

If person.name is null then the String “Pravin Kumar” is assigned to variable name.This simplifies code massively by reducing the need to check if name is null and then assigning something else instead.

??= simply means “If left hand side is null, carry out assignment”. This will only assign a value if the variable is null.

String name = null;
name ??= “Smazee”;
print(name); // Smazee

name ??= “Creative Haven”;
print(name); // Smazee

In the above example “Smazee” is assigned to variable name as it is null & not assigned at line 4 as it already holds a string. (not null)

This is not the end. These are the most used and popular operators and functions. To know about more about Dart documentation. You can also practice dart in Online Editor DartPad.

By on
dart flutter tips coding tricks
Smazee https://smazee.com/uploads/blog/Dart-Programming---Tips-and-Tricks.png

Read also

  1. Implement glassmorphism in your flutter app
  2. Boosting Business through Chat bots? How we took advantage.
  3. How a great website will help improve business?