LogoDart Beginner Tutorials

Hello World and Your First Dart Pet

We will create a simple Dart program to display a greeting and introduce fundamental concepts.

Welcome to your Dart adventure! In this first part, we'll build a simple Dart program—the classic "Hello, World!"—but with a twist. We'll think of this as our first interaction with our soon-to-be-interactive digital pet. Don't worry, it's going to be fun and easy!

Let's start by creating a new Dart file (you can name it main.dart). We'll write our very first Dart program in it.

Step 1: The main Function – The Heart of Your Program#

Every executable Dart program needs a main function. This is where the program starts running. Think of it as the entry point for your digital pet's world. Add this to your main.dart file:

void main() {
  // Our pet's world begins here!
}

void means this function doesn't return any value. main is a special function name.

Step 2: Saying Hello to the World (and Your Pet!)#

Let's get our pet to say hello! We'll use the print function to display text on the console. This is how we'll communicate with our pet (and vice versa, in later parts). Add the following inside the main function:

print('Hello, World!'); 

Run your code (using a Dart SDK). You should see "Hello, World!" printed in your terminal. Congratulations! You've written your first Dart program.

Step 3: Introducing String Variables – Giving Your Pet a Name (Placeholder for Now)#

We'll improve this. Instead of a generic greeting, let's give our pet a name (even if it's a temporary one for now). We'll use a String variable to store the pet's name. Add this above the print statement:

String petName = 'Fluffy';

Now, let's use this variable in our greeting. We'll modify the print statement:

print('Hello, $petName!'); 

This uses string interpolation—the $ sign before the variable name inserts its value directly into the string. Run your code again. You should see "Hello, Fluffy!"

Now you've used a variable to personalize your greeting. In the next part, we'll take things further by letting the user choose the pet's name!

What's Next?#

In the next part, "Feeding Your Digital Pet: Variables, Data Types, and User Input," we'll learn how to use different data types (like int for numbers), accept user input, and add more interactive elements to our digital pet simulation. Get ready to feed and care for your virtual friend!