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.
void main() {
print("Hello World!");
}
Output:
Hello World!
Let's break down what each part means:
void main() - This is the starting point where the execution of your program begins{} represent the beginning and ending of a block of codeprint("Hello World!"); - This prints "Hello World!" to the console;Negative
: Don't forget the semicolon at the end of each statement - it's required in Dart!
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.
void main()
{
var name = "John";
print(name);
}
Output:
John
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
Dart supports various data types:
var - Type inference (Dart determines the type)String - Text dataint - Integer numbersdouble - Decimal numbersbool - True/false valuesPositive
: 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.
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
+ for addition- for subtraction* for multiplication/ for division (returns double)~/ for integer division% for modulo (remainder)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.
dart create <project_name>
This command creates a simple Dart project with ready-made code and proper structure.
dart create project_name (e.g., dart create first_app)cd first_app to enter the project directorycode . to open the project in Visual Studio CodeWhen you create a Dart project, you'll get:
bin/ - Contains your main Dart fileslib/ - For your library codetest/ - For your test filespubspec.yaml - Project configuration and dependenciesPositive
: 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.
First, navigate to your project directory and run:
dart run
This command will:
# Run a specific file
dart run bin/main.dart
# Run with debugging
dart run --debug
# Run with profiling
dart run --profile
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!
Create a new Dart project with the name stockmanagement and implement a simple program that:
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:
To expand your Dart knowledge, consider learning about:
Positive
: You're now ready to build more complex Dart applications!
Negative
: Remember to practice regularly - programming is a skill that improves with use.
Create a simple calculator program that:
Good luck with your Dart programming journey!