Recap - User Profile System
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 29 of 110.
Challenge
EasyLet'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 aUserProfileclass 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
requiredforusernameandemail - 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, ornullif 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
- A non-nullable
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 to28 - 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 printIdentity: [getDisplayIdentity()], then printBio preview: [getBioPreview()]
- Create a user with username
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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor