Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. Functions
  2. Arrow Function

arrowFunctions

In Dart, arrow functions, also known as "fat arrow" functions, provide a concise way to define functions,
especially for short, one-liner functions. Arrow functions are often used for functions that have a single
expression and return a value. Here's how you can define an arrow function in Dart:
returnType functionName(parameters) => expression;

Let's break down the components of an arrow function:

  • returnType: Specifies the data type of the value that the function will return. If the function doesn't return a value, you can use void.

  • functionName: This is the name of the function.

  • parameters: These are the function's parameters, if any. Parameters are enclosed in parentheses ().

  • expression: This is the single expression that is evaluated and returned by the function. Arrow functions are concise because they automatically return the result of the expression.

Here are some examples of arrow functions in Dart:

Arrow Function with No Parameters:

void sayHello() => print('Hello, Dart!');

In this example, the sayHello arrow function does not have any parameters and directly prints a greeting message.

Arrow Function with Parameters:

int add(int a, int b) => a + b;

In this example, the add arrow function takes two parameters (a and b) and returns their sum as an int. The arrow function is concise and returns the result of the addition expression directly.

Arrow Function with Named Parameters:

String greet({String? firstName, String? lastName}) => 'Hello, ${firstName ?? 'User'} ${lastName ?? ''}!';

In this example, the greet arrow function uses named parameters (firstName and lastName) and returns a greeting message as a String. The arrow function is concise and constructs the message using the provided named parameters.

Arrow functions are especially useful for short, simple functions and can make your code more concise and readable. However, keep in mind that they are suitable for functions with a single expression. If your function requires multiple statements or complex logic, it's better to use a regular named function.

PreviousArrow FunctionNextBuiltinFunctions

Last updated 1 year ago