“Master your Dart Syntax!”

Anuj Kumar Pandey
2 min readAug 31, 2023

--

Flutter enthusiasts, gather ‘round! Today, we’re diving deep into Dart’s quirkiest and most specific rules. Don’t worry, we’ll keep it fun and humorous. Let’s decode Dart’s dramatic demands, shall we?

1. The Name Game:

Folders/files?

Stick to `lower_snake_case`. So, `my_awesome_folder` is the way to go, not `MyAwesomeFolder` or `myAwesomeFolder` or the cringy `my-awesome-folder`.

Classes?

Think tall! `UpperCamelCase` like `AmazingClass`.

Functions, variables, and extensions?

Go for `lowerCamelCase`. So, `myCoolFunction` and not `MyCoolFunction`.

Mixins?

Dart likes its mixins tall too. So, remember `UpperCamelCase` for mixins.

Constants?

Yelling is Key. Well, not quite.

// NO
CAPITALIZE_EVERY_DAMN_LETTER
// yes~
lowerCamelCase

Enum?

Got an enum named `Name`? The values should be `enumValue` not `ENUM_VALUE`. Please, Dart has feelings too.

// NO GOD NO! PLEASE NO!
enum Color { RED, GREEN }
// yes~
enum Color { red, green }

2. The Underscore Understudy:

If you won’t use a callback parameter, just replace it with `_` or `__`. It’s like that one friend who insists on coming to the party but just stands in the corner.

// Unnecessary
futureOfVoid.then((unusedParameter) => print('Done.'));

// Neat
futureOfVoid.then((_) => print('Completed'));

3. The String Story:

String interpolation is the classy way to compose strings and values. So, `’Hello, $name! You are ${year — birth} years old.’` is Dart’s favorite dialogue. Concatenation with +? That’s for peasants.

// Clean
'Hello, $name! You are ${year - birth} years old.'

// Complicated
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' years old.'

3. Getter-Setter Gossip:

Avoid unnecessary getters and setters, which you don’t even use. It’s like having a middleman in a direct conversation. Let’s keep it direct and simple.

4. The Type Tyrant:

Always specify types! `int add(int a, int b) => a + b;` is the golden rule. But don’t go overboard with defining types everywhere, it’s not a type show-off competition.

// Nope
add(a,b) => a + b;

// Now we're talking
int add(int a, int b) => a + b;

// Overdoing it
final List<String> users = <String>[];

// Just right
final List<String> users = [];

// Slick and simple
final users = <String>[];

5. The Prehistoric `new` Keyword:

Stop using the `new` keyword. Dart finds it as outdated as your grandpa’s fashion sense. Opt for the fresh and fabulous approach.

// Remember the 90s?
new Container();

// Welcome to the Future!
Container();

In conclusion, while Dart might come across as a bit dramatic, it just wants to be understood. Feel free to comment, share and like 👍 . I am also open for discussions, don’t worry I don’t bite (probably).

--

--