๐Ÿงบ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.

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 to get it set up.

Or you can simply use dart's online code editor dartpad.

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:

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:

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:

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

Loops:

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:

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.

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

Assignments ๐Ÿ“

By completing these assignments, you'll reinforce what you've learned and get more comfortable with Dart.

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:

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!

Last updated