0
0
Fluttermobile~20 mins

Null safety in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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);
AString? name = null; print(name?.length);
BString name = null; print(name.length);
CString name = 'Flutter'; print(name.length);
DString? name = 'Dart'; print(name!.length);
Attempts:
2 left
💡 Hint
Check if the variable type allows null values.
ui_behavior
intermediate
2: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');
}
ADisplays 'No message'
BDisplays nothing (empty space)
CThrows a runtime error
DDisplays 'null'
Attempts:
2 left
💡 Hint
Look at the null-coalescing operator usage.
lifecycle
advanced
2: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);
}
AThrows LateInitializationError
BPrints null
CPrints empty string
DPrints 'title'
Attempts:
2 left
💡 Hint
Consider what 'late' means in Dart null safety.
navigation
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);
APrints 0
BThrows a NoSuchMethodError
CThrows a TypeError
DPrints null
Attempts:
2 left
💡 Hint
Check the cast and null safety behavior.
🔧 Debug
expert
2: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);
  }
}
ABecause 'title' should be nullable String
BBecause 'initState' is missing super.initState() call
CBecause Text widget cannot display variables
DBecause 'title' is late but never initialized before use
Attempts:
2 left
💡 Hint
Check when 'title' is assigned a value.