What does adding ? after a type (e.g. String?) mean in Dart?
String? means the variable is nullable. It can hold either a String value or null. Without ?, the variable can never be null.
Write safer code with Dart null safety and null-aware operators
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Null safety prevents null pointer exceptions, which are crashes caused by using a variable that was never assigned a value.
In Dart, variables are non-nullable by default. You must explicitly opt in to nullable types using ?.
1// Non-nullable - must always have a value2String name = 'Alice';3int age = 25;45// Nullable - can hold null6String? nickname = null; // OK ✓7int? score; // Also null by default ✓89// Error: non-nullable cannot be null10// String city = null; // ❌ Compile error1112// ── Null-aware operators ──13String? user;1415// ?. - safe access (returns null instead of crashing)16print(user?.toUpperCase()); // null1718// ?? - null coalescing (fallback value)19String display = user ?? 'Guest';20print(display); // Guest2122// ??= - assign only if null23user ??= 'DefaultUser';24print(user); // DefaultUser2526// ! - null assertion (crash if null - use carefully!)27String definitelySet = 'Hello';28String? maybeSet = definitelySet;29print(maybeSet!.length); // 5
1// ── late keyword ──2// For non-nullable variables initialized after declaration3class ProfileScreen extends StatefulWidget {4late String userId; // Will be set before first use56void init(String id) {7userId = id;8}9}1011// Lazy initialization with late12late final List<String> data = _loadData(); // Only computed when first accessed1314List<String> _loadData() => ['item1', 'item2'];
Prefer using ?? and ?. over the ! operator. The ! operator defeats null safety. If you use it on a null value, your app crashes at runtime. Always provide a fallback with ?? when possible.
Use this mental model:
Most production crashes from null safety happen because ! is used to silence compiler warnings instead of fixing data flow.
Write safer code with Dart null safety and null aware operators. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Build confidence with basics first; speed comes later.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
String? means the variable is nullable. It can hold either a String value or null. Without ?, the variable can never be null.
The ?? operator is null coalescing. `a ?? b` returns a when a is not null, otherwise returns b as a fallback.
The ?. operator (null-aware access) safely returns null instead of crashing when the object is null.
late tells Dart that a non-nullable variable will be assigned before first use, bypassing the requirement to initialize at declaration.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue