Part 2: Back to Basics - The main Function & Input

Welcome back, aspiring Dart programmer! In Part 1, you got a taste of our completed Digital Pet Simulator. You ran it, played with it, and hopefully even tinkered with its code. That "big picture" view is super helpful. Now, we're going to embark on the "build it up" phase. This is where you truly understand each piece by starting from a blank slate and adding elements step-by-step. It’s like learning to draw by sketching basic shapes before adding details.


Step 1: Clearing the Canvas & The Essential main Function

Every Dart program starts its execution in a special function called main. It's the entry point, the first piece of code that Dart runs when your program kicks off.

Your Task:

// main.dart

void main() {
  print('Hello, Digital Pet World!');
}

Run it! Open your terminal in VS Code (if it's not already open) and type:

dart run main.dart

You should see:

Hello, Digital Pet World!

Congratulations! You've run your first minimal Dart program.


Step 2: Making It Interactive - Asking for Input

A static "Hello" is fine, but our digital pet needs a name! To get input from the user, we'll need to use some special tools provided by Dart.

Your Task:

import 'dart:io'; // Required for input/output operations

Prompt the user: Inside your main function, let's ask for the pet's name.

void main() {
  print('Welcome to Digital Pet Simulator!');
  stdout.write('What would you like to name your pet? ');
  // ... rest of the code will go here ...
}

Read user input and store it (Variables!): Now, we need to capture what the user types.

void main() {
  print('Welcome to Digital Pet Simulator!');
  stdout.write('What would you like to name your pet? ');
  String? petName = stdin.readLineSync(); // Read user input

  print('You named your pet: $petName');
}

Your main.dart should now look like this:

// main.dart
import 'dart:io';

void main() {
  print('Welcome to Digital Pet Simulator!');
  stdout.write('What would you like to name your pet? ');
  String? petName = stdin.readLineSync(); // Read user input

  print('You named your pet: $petName');
}

Run it! (dart run main.dart)


Step 3: Handling Missing Input (Control Flow - if statement)

Seeing null isn't very friendly for our pet simulator. What if the user doesn't enter a name? We should give the pet a default name! This is a perfect job for control flow, specifically an if statement. An if statement allows your program to make decisions: "IF this condition is true, THEN do this block of code."

Your Task:

void main() {
  print('Welcome to Digital Pet Simulator!');
  stdout.write('What would you like to name your pet? ');
  String? petName = stdin.readLineSync();

  // Control Flow: Check if the name is missing or empty
  if (petName == null || petName.trim().isEmpty) {
    petName = 'Buddy'; // Assign a default name
    print('No name entered, calling your pet $petName!');
  } else {
    // The 'else' block runs if the 'if' condition is false
    petName = petName.trim(); // Remove any extra spaces from the name
    print('Hello, $petName!');
  }
}

Your main.dart should now look like this:

// main.dart
import 'dart:io';

void main() {
  print('Welcome to Digital Pet Simulator!');
  stdout.write('What would you like to name your pet? ');
  String? petName = stdin.readLineSync(); // Read user input

  // Control Flow: Check if the name is missing or empty
  if (petName == null || petName.trim().isEmpty) {
    petName = 'Buddy'; // Assign a default name
    print('No name entered, calling your pet $petName!');
  } else {
    // The 'else' block runs if the 'if' condition is false
    petName = petName.trim(); // Remove any extra spaces from the name
    print('Hello, $petName!');
  }
}

Run it! (dart run main.dart)

You've now got a program that greets your pet by name, handles missing input, and uses core concepts like variables, data types (String?), I/O, and conditional control flow!


Step 4: Setting the Stage - Displaying a Static Status

Now that we have a name, let's give the user a preview of what we're building towards: a status screen for the pet. For now, we'll just add a "hardcoded" block of text. This isn't dynamic yet, but it sets the stage for what we'll build in Part 3.

Your Task:

// Display a hardcoded status for now
print('\n--- ${petName}\'s Status ---');
print('Hunger: [#####-----]');
print('Happiness: [#####-----]');
print('-------------------------');

You now have a program that greets the pet by name and shows a placeholder for its status. This is the perfect foundation for the next part!


Challenge for You:

Can you add another print statement after the if-else block that always displays the final chosen name of the pet, regardless of whether it was typed or defaulted to 'Buddy'?


What's Next?

In Part 3, we'll start thinking about how to organize our code as our program grows. We'll introduce functions to make code reusable and, crucially, delve into classes and objects – the building blocks for our DigitalPet!

Back to Digital Pet Overview Previous: Part 1 Next: Part 3