Challenge - 5 Problems
StatelessWidget Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What will this StatelessWidget display?
Consider this Flutter code for a simple stateless widget. What text will appear on the screen?
Flutter
import 'package:flutter/material.dart'; class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return const Text('Hello Flutter'); } } void main() { runApp(const MaterialApp(home: Scaffold(body: Center(child: MyWidget())))); }
Attempts:
2 left
💡 Hint
Look at the Text widget inside the build method.
✗ Incorrect
The build method returns a Text widget with the string 'Hello Flutter'. This text will be displayed centered on the screen.
❓ lifecycle
intermediate2:00remaining
What happens when a StatelessWidget's data changes?
If you change a variable used inside a StatelessWidget, what happens when you call setState() in the parent widget?
Attempts:
2 left
💡 Hint
StatelessWidgets rebuild when their parent rebuilds.
✗ Incorrect
StatelessWidgets rebuild when their parent widget rebuilds and passes new data. They do not hold internal state but can display updated data from parents.
📝 Syntax
advanced2:00remaining
Which code snippet correctly defines a StatelessWidget?
Choose the correct Flutter code that defines a valid StatelessWidget named GreetingWidget that shows 'Hi there!'.
Attempts:
2 left
💡 Hint
Check the class inheritance and build method signature.
✗ Incorrect
Option C correctly extends StatelessWidget and overrides build with correct signature returning Text('Hi there!'). Option C misses BuildContext parameter. Option C extends StatefulWidget incorrectly. Option C returns wrong text.
🔧 Debug
advanced2:00remaining
Why does this StatelessWidget cause a runtime error?
This code causes an error when run. What is the cause?
Flutter
import 'package:flutter/material.dart'; class MyWidget extends StatelessWidget { final String? text; const MyWidget({Key? key, required this.text}) : super(key: key); @override Widget build(BuildContext context) { return Text(text!); } } void main() { runApp(const MaterialApp( home: Scaffold( body: MyWidget(text: null) ) )); }
Attempts:
2 left
💡 Hint
Look at the use of the exclamation mark (!) on a nullable variable.
✗ Incorrect
The code uses text! which forces a null check. Since text is null, this causes a runtime exception.
🧠 Conceptual
expert2:00remaining
Why choose StatelessWidget over StatefulWidget?
Which reason best explains why you would use a StatelessWidget instead of a StatefulWidget in Flutter?
Attempts:
2 left
💡 Hint
StatelessWidgets are simple and do not hold changing data.
✗ Incorrect
StatelessWidgets are used when the UI depends only on the input parameters and does not change internally. StatefulWidgets are needed for dynamic data or animations.