# Dart Basics

Welcome to the world of Flutter! Before diving into Flutter, it's essential to understand its backbone - the Dart language. Let’s take the first step towards mastering Flutter by learning the basics of Dart!

Here's an optional video, either you can watch this video tutorial or just go through the documentation below.

{% embed url="<https://youtu.be/dwZGV_1tJwc>" fullWidth="false" %}
dart tutorial
{% endembed %}

### 1. Installing Dart 💻

Dart is the programming language used for building applications in Flutter. First, you'll need to install Dart on your machine. Follow the [official Dart installation guide](https://dart.dev/get-dart) to get it set up.

Or you can simply use dart's online code editor [dartpad](https://dartpad.dev/).

### 2. Your First Dart Program 🎉

Once Dart is installed, it's time to write your first Dart program. Create a new file called `main.dart` and type the following code:

```dart
void main() {
  print('Hello, Dart!');
}
```

Save the file and run it by typing `dart main.dart` in your terminal. You should see `Hello, Dart!` printed to the console. The `main()` function is the entry point of a Dart program, and `print()` is a function that outputs text to the console.

### 3. Variables and Data Types 🧮

Variables are used to store data that can be used and manipulated throughout a program. In Dart, you can define a variable with the `var` keyword, or specify its type for added clarity:

```dart
void main() {
  var name = 'Flutter University';  // Inferred as String
  String greeting = 'Welcome to ';
  print(greeting + name);  // Output: Welcome to Flutter University
}
```

### 4. Control Flow 🔄

Control flow allows you to create paths in your code that will only be executed under certain conditions. This is done using statements like `if`, `else`, `for`, `while`, and `switch`.

#### If-Else Statements:

```dart
void main() {
  var score = 85;
  if (score >= 90) {
    print('Excellent');
  } else if (score >= 75) {
    print('Good');
  } else {
    print('Keep trying');
  }
}
```

#### Loops:

```dart
void main() {
  // For loop
  for (var i = 0; i < 5; i++) {
    print('Hello $i');
  }

  // While loop
  var j = 0;
  while (j < 5) {
    print('World $j');
    j++;
  }
}
```

### 5. Functions ⚙️

Functions are blocks of code that perform a specific task and can be used repeatedly in a program. Here's how you can define and call a simple function to add two numbers:

```dart
void main() {
  int add(int a, int b) {
    return a + b;
  }

  print(add(10, 20));  // Output: 30
}
```

### 6. Error Handling 🚫

Errors can occur while your program is running. It's important to handle these errors to ensure your program doesn't crash. Dart uses `try`, `catch`, and `finally` blocks to handle errors.

```dart
void main() {
  try {
    var result = 10 ~/ 0;
  } catch (e) {
    print(e);  // Output: IntegerDivisionByZeroException
  } finally {
    print('Done');
  }
}
```

***

### Assignments 📝

{% hint style="info" %}
By completing these assignments, you'll reinforce what you've learned and get more comfortable with Dart.
{% endhint %}

Now that you've learned the basics of Dart, it's time to put your knowledge to the test. Here are some assignments for you:

* [ ] Create a program that prints a greeting.
* [ ] Write a function that calculates the area of a rectangle.
* [ ] Create a loop that prints numbers 1 to 10.
* [ ] Write an if-else statement that prints whether a number is even or odd.
* [ ] Handle a division by zero error with a try-catch block.

Once you're ready, you can proceed to the next topic: Flutter Installation and Setup. Remember, practice is the key to mastering any programming language!
