Challenge - 5 Problems
Arrow Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate1:30remaining
Identify the output of a simple arrow function
What is the output of this Flutter Dart code when the button is pressed?
Flutter
void main() { final greet = () => 'Hello Flutter!'; print(greet()); }
Attempts:
2 left
💡 Hint
Arrow functions return the expression after the arrow automatically.
✗ Incorrect
The arrow syntax (=>) returns the expression directly. So greet() returns 'Hello Flutter!'.
❓ ui_behavior
intermediate2:00remaining
Arrow function in Flutter onPressed callback
Which option correctly uses an arrow function to update the text when a button is pressed in Flutter?
Flutter
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { String text = 'Before'; void updateText() => setState(() => text = 'After'); @override Widget build(BuildContext context) { return ElevatedButton( onPressed: updateText, child: Text(text), ); } }
Attempts:
2 left
💡 Hint
setState must be called to update UI; arrow syntax can be nested.
✗ Incorrect
Option D uses arrow syntax for updateText and inside setState to update text and refresh UI correctly.
❓ lifecycle
advanced1:30remaining
Arrow function in Flutter widget lifecycle method
What happens if you replace this initState method with arrow syntax in a StatefulWidget?
Flutter
class _MyState extends State<MyWidget> { @override void initState() { super.initState(); print('Init'); } }
Attempts:
2 left
💡 Hint
Arrow functions can only have one expression, but initState has two statements.
✗ Incorrect
initState has two statements: super.initState() and print(). Arrow syntax only supports one expression, so this causes a syntax error.
advanced
1:30remaining
Arrow function in Navigator callback
Which option correctly uses arrow syntax to navigate to a new screen on button press?
Flutter
ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen()));
},
child: Text('Go'),
)Attempts:
2 left
💡 Hint
Arrow functions can omit braces if only one expression is executed.
✗ Incorrect
Option A uses arrow syntax correctly for a single expression. Option A uses braces with arrow syntax which is invalid. Option A calls Navigator.push immediately, not on button press.
🔧 Debug
expert2:00remaining
Debugging arrow function with async in Flutter
What error occurs with this arrow function in Flutter and why?
Flutter
Future<void> fetchData() async { await Future.delayed(Duration(seconds: 1)); print('Data loaded'); };
Attempts:
2 left
💡 Hint
Arrow functions can only have one expression, not multiple statements in braces.
✗ Incorrect
The arrow function body cannot be a block with multiple statements inside braces. This causes a syntax error.