Menu
Coddy logo textTech

Recap - User Profile System

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 29 of 110.

challenge icon

Challenge

Easy

Let's build a complete user profile system that brings together all the null safety concepts you've learned in this chapter. You'll create a system that manages user accounts with a mix of required information and optional details.

You'll organize your code into two files:

  • user_profile.dart: Define a UserProfile class that represents a user account. Think about what information is essential (like a username) versus what's optional (like a bio or phone number). Your class should have:
    • A non-nullable String username - every user must have a username
    • A non-nullable String email - every user must have an email
    • A nullable String? displayName - users might prefer to show a different name
    • A nullable String? bio - not everyone writes a bio
    • A nullable int? age - age is optional personal information
    • A constructor using named parameters with required for username and email
    • A method getDisplayIdentity() that returns the display name if set, otherwise returns the username using ??
    • A method getBioPreview() that returns the first 20 characters of the bio followed by ... if the bio exists and is longer than 20 characters, the full bio if it's 20 characters or less, or null if no bio exists (use ?. for safe access)
    • A method setDefaultBio() that sets the bio to 'Hello, I am new here!' only if it's currently null using ??=
    • A method printProfile() that displays the user's complete profile
  • main.dart: Import your user profile class and create several users to demonstrate the full range of null safety features:
    • Create a user with username 'alice_dev' and email 'alice@code.com', then set display name to 'Alice', bio to 'Software developer passionate about clean code and testing', and age to 28
    • Create a user with username 'bob_builder' and email 'bob@build.io', then set only the display name to 'Bob'
    • Create a user with username 'charlie99' and email 'charlie@mail.net' (leave all optional fields as null)
    • Call setDefaultBio() on all three users
    • For each user in order, call printProfile(), then print Identity: [getDisplayIdentity()], then print Bio preview: [getBioPreview()]

The printProfile() method should print in this format:

===== User Profile =====
Username: [username]
Email: [email]
Display Name: [displayName or "Not set"]
Bio: [bio or "No bio"]
Age: [age or "Not specified"]

Notice how Alice's bio stays unchanged after setDefaultBio() because it already has a value, while Charlie gets the default bio assigned. Also observe how the bio preview truncates Alice's longer bio but shows Charlie's shorter default bio in full.

Expected output:

===== User Profile =====
Username: alice_dev
Email: alice@code.com
Display Name: Alice
Bio: Software developer passionate about clean code and testing
Age: 28
Identity: Alice
Bio preview: Software developer p...
===== User Profile =====
Username: bob_builder
Email: bob@build.io
Display Name: Bob
Bio: Hello, I am new here!
Age: Not specified
Identity: Bob
Bio preview: Hello, I am new here!
===== User Profile =====
Username: charlie99
Email: charlie@mail.net
Display Name: Not set
Bio: Hello, I am new here!
Age: Not specified
Identity: charlie99
Bio preview: Hello, I am new here!

Try it yourself

import 'user_profile.dart';

void main() {
  // TODO: Create user 'alice_dev' with email 'alice@code.com'
  // Set displayName to 'Alice'
  // Set bio to 'Software developer passionate about clean code and testing'
  // Set age to 28
  
  // TODO: Create user 'bob_builder' with email 'bob@build.io'
  // Set only displayName to 'Bob'
  
  // TODO: Create user 'charlie99' with email 'charlie@mail.net'
  // Leave all optional fields as null
  
  // TODO: Call setDefaultBio() on all three users
  
  // TODO: For each user in order (alice, bob, charlie):
  // 1. Call printProfile()
  // 2. Print "Identity: [getDisplayIdentity()]"
  // 3. Print "Bio preview: [getBioPreview()]"
}

All lessons in Object Oriented Programming