Shortcuts in Dart to Boost Productivity | 1

 # Shortcuts in Dart to Boost Productivity


## Introduction

Dart is a powerful and rapidly evolving programming language primarily used for Flutter development. It provides several shortcuts that help developers write cleaner and more efficient code. In this blog, we will explore some useful Dart shortcuts.


---

## 1. Using Arrow Functions

Instead of writing functions with `{ return ...; }`, you can simplify them using the `=>` operator:


```dart

// Traditional method

int sum(int a, int b) {

  return a + b;

}


// Using arrow function

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

```


---

## 2. Ternary Operator

Used to shorten simple `if-else` statements:


```dart

// Traditional method

String status(int score) {

  if (score >= 50) {

    return "Pass";

  } else {

    return "Fail";

  }

}


// Using ternary operator

String status(int score) => score >= 50 ? "Pass" : "Fail";

```


---

## 3. Using `??` for Default Values

The `??` operator assigns a default value if a variable is `null`:


```dart

String? name;

print(name ?? "Name not available"); // Output: Name not available

```


---

## 4. Using `??=` to Assign a Value Only If `null`


```dart

String? userName;

userName ??= "Unknown User";

print(userName); // Output: Unknown User

```


---

## 5. Using `cascade notation` for Method Chaining

Instead of calling multiple functions on the same object separately, use `..`:


```dart

class Person {

  String? name;

  int? age;

  void greet() => print("Hello, I am $name and I am $age years old");

}


void main() {

  var person = Person()

    ..name = "Khaled"

    ..age = 25

    ..greet();

}

```


---

## Conclusion

These are some useful shortcuts in Dart that help you write more efficient and readable code. Do you have other shortcuts you use? Share them in the comments!