Recap - Handling Null Safely
Part of the Fundamentals section of Coddy's Dart journey — lesson 82 of 94.
Challenge
EasyYou're building a user profile system for a mobile app. Create a UserProfile class that demonstrates your understanding of Dart's null safety features.
Implement a class UserProfile with the following:
- Non-nullable properties (must always have a value):
idof typeintcreatedDateof typeString
- Nullable properties (can be null at initialization):
nameof typeString?emailof typeString?ageof typeint?
- Late-initialized property:
displayNameof typeString, declared using thelatekeyword
- Constructor:
- Accepts
idandcreatedDateas required parameters - Initializes
displayNameto"Guest"
- Accepts
Method:
<strong>void printProfile()</strong>Prints the user profile with the following format:User Profile: ID: <id> Created: <createdDate> Name: <name or 'Not provided'> Email: <email or 'Not provided'> Age: <age or 'Not provided'> Display Name: <displayName> -------------------- Method:
<strong>void updateEmail(String? newEmail)</strong>- Updates the
emailproperty with the new value.
- Updates the
- Method:
<strong>String getDisplayName()</strong>- If
nameis not null, setsdisplayName = name!(using null assertion operator) - Returns the current value of
displayName
- If
Important: After updating the name property, you must call getDisplayName() to update the displayName field before printing the profile again.
Complete the code to create a profile, update it, and display the information as shown in the expected output.
Try it yourself
void main() {
// Create a user profile
final profile = UserProfile(1, 'today');
// Print initial profile
profile.printProfile();
// Update profile
profile.name = 'John Doe';
profile.updateEmail('john@example.com');
profile.age = 30;
// Update display name and print updated profile
profile.getDisplayName(); // Call this to update displayName
profile.printProfile();
}
class UserProfile {
// TODO: Add properties (id, createdDate, name, email, age, displayName)
// TODO: Create constructor
// TODO: Implement printProfile() method
// TODO: Implement updateEmail() method
// TODO: Implement getDisplayName() method
}All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number