šÆ Project: Personal Greeting App
šÆ Objective
Build a command-line program that interacts with the user and performs basic calculations using everything you learned in Lesson 1.
Requirements
- Create a new Dart file called
greeting_app.dartin your project'sbinfolder - Ask for user's name using
stdin.readLineSync()(you'll need toimport 'dart:io';) - Ask for their age and convert it to an integer
- Calculate approximate birth year using current year minus age
- Print a personalized greeting including their name and calculated birth year
- Handle invalid input (empty name, non-numeric age, unrealistic ages)
- Add a fun message based on their age group (minor, adult, senior)
Expected Output
Terminal
=== Personal Greeting App ===
What is your name?
> Alice
How old are you?
> 25
Hello, Alice! š
You were born around the year 2001.
You are an adult. Perfect time to learn Dart!
Terminal (Error Handling)
=== Personal Greeting App ===
What is your name?
>
Error: Name cannot be empty. Please try again.
Step-by-Step Hints
š” Hint 1: Getting Started
Start with import 'dart:io'; at the top. This gives you access to stdin.readLineSync() for reading user input.
š” Hint 2: Safe Number Conversion
Use int.tryParse(input) instead of int.parse(input). tryParse returns null if the conversion fails instead of throwing an error.
š” Hint 3: Getting Current Year
Use DateTime.now().year to get the current year dynamically.
Challenge: Extend the Project
Once you've completed the basic requirements, try these extensions:
- Ask for the user's birth month and calculate a more accurate age
- Calculate how many days until their next birthday
- Store multiple users' information and display all at the end
- Add color to the output using ANSI escape codes
š Well Done!
You've completed your first Dart project! This demonstrates understanding of variables, user input, type conversion, conditional logic, and string interpolation - all fundamental Dart concepts.