DEV Community

ThinkDigitalSoftware
ThinkDigitalSoftware

Posted on • Updated on

Fun Dart Tips

Abbreviate variable declarations:
The following is a valid dart way of declaring multiple variables in the same statement:

String managers = "Managers",
      teachers = "Teachers",
      families = "Families";

Run an anonymous function in places where you can only use a oneliner. For example, a parameter inside a constructor:

var status = "Arriving";
var greeting = (){
   switch(a) {
     case "Arriving":
       return "Hi!";
     case "Leaving":
       return "Bye!";
     case "Staying":
       return "Make yourself at home!";
     default:
       return "hmmmm";
    }
}() // <-- note. You have to include this ending parenthesis 
//or the function will never evaluate. You're calling the function here. 
//Just like any other function, you have to include the parenthesis in order to run it.

Assigning a value to a variable returns that value.
So you can return assignments and it will print the assigned value.

List a = [1, 0, 0];
print((a[1] = 2)); // prints 2

The following also works.

String b;
print(b = "This is what I'm assigning to 'b'"); 
// prints "This is what I'm assigning to 'b'

When you print(a..[2] = 3),
It assigns the value and also returns the entire list. This allows for you to return a list while modifying it...

print((a..[2] = 3)); // prints [1, 2, 3]

List c = a..[0] = 9999999;
print(c); // prints [9999999, 2, 3]

These are just a few that I have right now. I'll add more as I come up with them!

Top comments (2)

Collapse
 
creativ_bracket profile image
Jermaine

How about the spread operator and collection if/for elements?

Collapse
 
thinkdigitalsoftware profile image
ThinkDigitalSoftware

Those are definitely fun! I may add them later when I get more time. I do have them in a medium article somewhere