0
0
Fluttermobile~20 mins

StatelessWidget in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StatelessWidget Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()))));
}
AHello World
BHello Flutter
CError: Missing build method
DBlank screen
Attempts:
2 left
💡 Hint
Look at the Text widget inside the build method.
lifecycle
intermediate
2: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?
AThe StatelessWidget rebuilds with new data
BThe StatelessWidget updates its internal state automatically
CNothing happens because StatelessWidget cannot rebuild
DThe app crashes with an error
Attempts:
2 left
💡 Hint
StatelessWidgets rebuild when their parent rebuilds.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines a StatelessWidget?
Choose the correct Flutter code that defines a valid StatelessWidget named GreetingWidget that shows 'Hi there!'.
A
class GreetingWidget extends StatefulWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hi there!');
  }
}
B
class GreetingWidget extends StatelessWidget {
  Widget build() {
    return Text('Hi there!');
  }
}
C
class GreetingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hi there!');
  }
}
D
class GreetingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello');
  }
}
Attempts:
2 left
💡 Hint
Check the class inheritance and build method signature.
🔧 Debug
advanced
2: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)
    )
  ));
}
AConstructor missing super.key parameter
BMissing override annotation on build method
CText widget cannot accept nullable strings
DNull check operator used on a null value causing an exception
Attempts:
2 left
💡 Hint
Look at the use of the exclamation mark (!) on a nullable variable.
🧠 Conceptual
expert
2:00remaining
Why choose StatelessWidget over StatefulWidget?
Which reason best explains why you would use a StatelessWidget instead of a StatefulWidget in Flutter?
AWhen the widget does not need to manage or update any internal state
BWhen you want to update the UI based on user interaction
CWhen you need to store data that changes over time
DWhen you want to use animations or timers inside the widget
Attempts:
2 left
💡 Hint
StatelessWidgets are simple and do not hold changing data.