Arrow Functions in Dart

Arrow Functions, Dart, Google Flutter -

Arrow Functions in Dart


Dart offers arrow functions, which enable the developer to shorten single-line functions that calculate & return something.

You can use:
=> xxx

instead of:
{ return xxx; }

Arrow functions are often used by event handlers and when you set state (more on that later).

Lets Run Some Code

num divideNonLambda(num arg1, num arg2) {
  return arg1 / arg2;

num divideLambda(num arg1, num arg2) => arg1 / arg2;

void main() {
  print('non-lambda ${divideNonLambda(6, 2)}');
  print('non-lambda ${divideNonLambda(9, 2)}');
  print('non-lambda ${divideNonLambda(9, 2.5)}');

  print('lambda ${divideLambda(6, 2)}');
  print('lambda ${divideLambda(9, 2)}');
  print('lambda ${divideLambda(9, 2.5)}');

What Happens When You Run the Code

non-lambda 3
non-lambda 4.5
non-lambda 3.6
lambda 3
lambda 4.5
lambda 3.6