FMT – 3 (What is Arrow Syntax (Fat Arrow) and How To Use It?)

I had the opportunity to work with many different programming languages. However, I can say that the arrow syntax in Dart, which you can use while programming in Flutter, is a completely unique structure. Of course, there are some other shorthand expressions. But with the help of arrow syntax -it is also known as Fat Arrow-, you can easily define single-line functions.

The structure of a function defined using the arrow syntax will be as follows.

returnType FunctionName(Parameters) => Expression;

As it can be seen that, there is no curly braces or the ‘return’ keyword any more.

Now, lets see this usage with an example. Assume we have function as follows.

void DummyFunction(int x){
  if (x == 0) {
    print('x is zero');
  } else {
    print('x is not zero');

The same function can be easily defined with arrow syntax.

void DummyFunction(int x) => print('x is ${x == 0 ? '' : 'not '}zero');

It is really useful for writing shorter codes.

Happy coding…

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.