Non-Nullable Types
Part of the Fundamentals section of Coddy's Dart journey — lesson 79 of 94.
Non-nullable types in Dart are the default for all variables. When you declare a variable without the ? symbol, it must always have a value and cannot be null.
Create a non-nullable String variable:
String name = 'Dart';
print(name);After executing the above code, the output will be:
DartAttempting to assign null to a non-nullable variable causes an error:
// This would cause an error:
// String language = null;Non-nullable variables must be initialized with a value:
int score = 100;
bool isActive = true;
double price = 9.99;
print('Score: $score, Active: $isActive, Price: $price');After executing the above code, the output will be:
Score: 100, Active: true, Price: 9.99Challenge
BeginnerIn this challenge, you'll practice working with non-nullable types in Dart. By default, variables in Dart cannot be null unless you explicitly make them nullable using the ? symbol.
Your task is to fix the code by assigning appropriate values to the non-nullable variables that are currently uninitialized.
Expected output:
Name: John
Age: 25
Is active: true
Score: 95.5Cheat sheet
Non-nullable types in Dart are the default for all variables. They must always have a value and cannot be null.
Declare non-nullable variables with initial values:
String name = 'Dart';
int score = 100;
bool isActive = true;
double price = 9.99;Non-nullable variables cannot be assigned null:
// This would cause an error:
// String language = null;Use string interpolation to display variable values:
print('Score: $score, Active: $isActive, Price: $price');Try it yourself
void main() {
// These variables are non-nullable and need values
// TODO: Initialize these variables with appropriate values
String name;
int age;
bool isActive;
double score;
// This code will print the values
print('Name: $name');
print('Age: $age');
print('Is active: $isActive');
print('Score: $score');
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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