Challenge - 5 Problems
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Identify the correct way to define a named parameter in a Flutter constructor
Which constructor correctly uses a named parameter for the
title property in a Flutter widget class?Flutter
class MyWidget extends StatelessWidget { final String title; MyWidget({this.title}); @override Widget build(BuildContext context) { return Text(title); } }
Attempts:
2 left
💡 Hint
Remember that named parameters use curly braces and can be marked as required.
✗ Incorrect
Option A correctly defines a named parameter
title with the required keyword, ensuring it must be provided. Option A uses a positional parameter. Option A is invalid syntax. Option A defines a named parameter but without this. and without required, which is not recommended.❓ ui_behavior
intermediate2:00remaining
What will be displayed when using a constructor with a default named parameter?
Given this widget code, what text will appear on the screen if
MyWidget() is used without arguments?Flutter
class MyWidget extends StatelessWidget { final String title; MyWidget({this.title = 'Hello'}); @override Widget build(BuildContext context) { return Text(title); } }
Attempts:
2 left
💡 Hint
Check the default value assigned to the named parameter.
✗ Incorrect
The constructor sets a default value 'Hello' for the named parameter
title. If no argument is passed, the default is used, so the text 'Hello' appears.❓ lifecycle
advanced2:00remaining
How does using named parameters with
required affect widget rebuilds?Consider a Flutter widget with a constructor using named parameters marked as
required. What is the impact on widget rebuilds when the parameter values change?Attempts:
2 left
💡 Hint
Think about how Flutter compares widget properties to decide rebuilds.
✗ Incorrect
Flutter rebuilds widgets when their properties change. Using named parameters marked as
required ensures those properties are set and tracked. If their values change, Flutter triggers a rebuild.advanced
2:00remaining
How to pass named parameters when navigating to a new screen in Flutter?
You want to navigate to
DetailScreen and pass a named parameter id. Which code correctly passes the parameter?Flutter
class DetailScreen extends StatelessWidget { final int id; DetailScreen({required this.id}); @override Widget build(BuildContext context) { return Text('ID: $id'); } }
Attempts:
2 left
💡 Hint
Check how named parameters are passed in constructors inside the builder function.
✗ Incorrect
Option B correctly uses the named parameter
id when creating DetailScreen. Option B passes a positional parameter which is invalid. Option B passes arguments but not as named parameters in constructor. Option B uses an invalid parameter id in pushNamed.🔧 Debug
expert2:00remaining
Why does this Flutter constructor cause a runtime error?
Examine this Flutter widget constructor. Why does it cause a runtime error when instantiated without arguments?
Flutter
class MyWidget extends StatelessWidget { final String title; MyWidget({required this.title}); @override Widget build(BuildContext context) { return Text(title); } } // Usage: // MyWidget();
Attempts:
2 left
💡 Hint
Check the meaning of
required in named parameters.✗ Incorrect
The constructor marks
title as required, so it must be provided when creating an instance. Calling MyWidget() without title causes a runtime error.