Challenge - 5 Problems
Null Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Identify the null safety error
Which option will cause a compile-time error due to null safety in Flutter?
Flutter
String name = null; print(name.length);
Attempts:
2 left
💡 Hint
Check if the variable type allows null values.
✗ Incorrect
Option B declares a non-nullable String but assigns null, which is not allowed in null safety.
❓ ui_behavior
intermediate2:00remaining
Null safety impact on UI rendering
What will be displayed when this Flutter widget runs?
Flutter
String? message; Widget build(BuildContext context) { return Text(message ?? 'No message'); }
Attempts:
2 left
💡 Hint
Look at the null-coalescing operator usage.
✗ Incorrect
The '??' operator provides a fallback string when 'message' is null, so 'No message' is shown.
❓ lifecycle
advanced2:00remaining
Null safety and late initialization
What happens if you access a 'late' variable before it is initialized?
Flutter
late String title; void printTitle() { print(title); }
Attempts:
2 left
💡 Hint
Consider what 'late' means in Dart null safety.
✗ Incorrect
Accessing a 'late' variable before initialization causes a runtime LateInitializationError.
advanced
2:00remaining
Null safety in route arguments
Given this code, what will happen if 'args' is null?
Flutter
final args = ModalRoute.of(context)?.settings.arguments as String; print(args.length);
Attempts:
2 left
💡 Hint
Check the cast and null safety behavior.
✗ Incorrect
Casting null to String with 'as String' causes a TypeError at runtime.
🔧 Debug
expert2:00remaining
Debugging null safety error in widget state
Why does this Flutter stateful widget throw a null safety error?
Flutter
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { late String title; @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return Text(title); } }
Attempts:
2 left
💡 Hint
Check when 'title' is assigned a value.
✗ Incorrect
The 'late' variable 'title' is used in build without initialization, causing LateInitializationError.