Welcome to the Basic Dart Program Tutorial! This codelab will guide you through the fundamentals of Dart programming language. Dart is a client-optimized language for fast apps on any platform, developed by Google.

What you'll learn:

Prerequisites:

Positive
: This tutorial is perfect for beginners who want to start their journey with Dart programming!

Let's start with the most basic Dart program - the classic "Hello World" program. This is the traditional first program that most programmers write when learning a new language.

Basic Hello World Program

void main() { 
   print("Hello World!"); 
}

Output:

Hello World!

Understanding the Code Structure

Let's break down what each part means:

Negative
: Don't forget the semicolon at the end of each statement - it's required in Dart!

Try It Yourself

Create a new file called hello.dart and add the code above. Then run it using:

dart hello.dart

Positive
: Congratulations! You've written your first Dart program!

Now let's learn how to work with variables in Dart. Variables are containers for storing data values.

Basic Variable Declaration

void main()
{
    var name = "John";
    print(name);
}

Output:

John

String Interpolation

Dart provides a powerful feature called string interpolation for joining variables. Use $variableName to include variables in strings:

void main(){
  var firstName = "John";
  var lastName = "Doe";
  print("Full name is $firstName $lastName");
}

Output:

Full name is John Doe

Variable Types

Dart supports various data types:

Positive
: Dart is a strongly typed language, but you can use var for type inference!

Let's learn how to perform basic mathematical operations in Dart.

Arithmetic Operations

void main() {
int num1 = 10; //declaring number1
int num2 = 3; //declaring number2
  
// Calculation
int sum = num1 + num2;
int diff = num1 - num2;
int mul = num1 * num2;
double div = num1 / num2; // It is double because it outputs number with decimal.
  
// displaying the output
print("The sum is $sum");
print("The diff is $diff");
print("The mul is $mul");
print("The div is $div");
}

Output:

The sum is 13
The diff is 7
The mul is 30
The div is 3.3333333333333335

Key Points:

Negative
: Division always returns a double, even when dividing integers!

For larger projects, it's better to create a proper Dart project structure. This helps manage configurations, packages, and assets.

Creating a New Dart Project

dart create <project_name>

This command creates a simple Dart project with ready-made code and proper structure.

Steps to Create a Dart Project

  1. Open your terminal/command prompt
  2. Navigate to your desired folder location
  3. Type dart create project_name (e.g., dart create first_app)
  4. Type cd first_app to enter the project directory
  5. Type code . to open the project in Visual Studio Code

Project Structure

When you create a Dart project, you'll get:

Positive
: The main Dart file will be in bin/first_app.dart (or whatever you named your project)

Now let's learn how to run your Dart projects.

Running a Dart Project

First, navigate to your project directory and run:

dart run

This command will:

Alternative Commands

# Run a specific file
dart run bin/main.dart

# Run with debugging
dart run --debug

# Run with profiling
dart run --profile

Converting Dart to JavaScript

Dart can be compiled to JavaScript for web applications:

dart compile js filename.dart

This creates a JavaScript file that can be run with Node.js.

Positive
: Dart's ability to compile to JavaScript makes it great for both client and server-side development!

Now it's time to practice what you've learned!

Challenge: Create a Stock Management Program

Create a new Dart project with the name stockmanagement and implement a simple program that:

  1. Declares variables for product name, quantity, and price
  2. Calculates the total value (quantity × price)
  3. Uses string interpolation to display the results
  4. Includes basic error checking

Sample Solution Structure

void main() {
  String productName = "Laptop";
  int quantity = 5;
  double price = 999.99;
  
  double totalValue = quantity * price;
  
  print("Product: $productName");
  print("Quantity: $quantity");
  print("Price per unit: \$$price");
  print("Total value: \$${totalValue.toStringAsFixed(2)}");
}

Negative
: Don't peek at the solution until you've tried it yourself!

Positive
: Practice is the best way to learn programming!

Congratulations! You've completed the Basic Dart Program Tutorial. Here's what you've accomplished:

What You've Learned

Continue Your Learning Journey

To expand your Dart knowledge, consider learning about:

Resources

Positive
: You're now ready to build more complex Dart applications!

Negative
: Remember to practice regularly - programming is a skill that improves with use.

Final Challenge

Create a simple calculator program that:

Good luck with your Dart programming journey!