0
0
Fluttermobile~20 mins

Functions and arrow syntax in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arrow Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
1: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());
}
AHello
Bnull
CHello Flutter!
DError: Missing return statement
Attempts:
2 left
💡 Hint
Arrow functions return the expression after the arrow automatically.
ui_behavior
intermediate
2: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),
    );
  }
}
Avoid updateText() { text = 'After'; }
Bvoid updateText() { setState(() { text = 'After'; }); }
Cvoid updateText() => text = 'After';
Dvoid updateText() => setState(() => text = 'After');
Attempts:
2 left
💡 Hint
setState must be called to update UI; arrow syntax can be nested.
lifecycle
advanced
1: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');
  }
}
APrints nothing because arrow functions do not run in lifecycle
BSyntax error because initState cannot be an arrow function with multiple statements
CPrints 'Init' correctly when widget initializes
DDoes not call super.initState(), causing runtime error
Attempts:
2 left
💡 Hint
Arrow functions can only have one expression, but initState has two statements.
navigation
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'),
)
AonPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen())),
BonPressed: () => { Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen())); },
ConPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen())); },
DonPressed: Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen())),
Attempts:
2 left
💡 Hint
Arrow functions can omit braces if only one expression is executed.
🔧 Debug
expert
2: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');
};
ASyntax error: Arrow function body cannot be a block with multiple statements
BNo error; prints 'Data loaded' after 1 second
CRuntime error: await used outside async function
DPrints immediately without delay
Attempts:
2 left
💡 Hint
Arrow functions can only have one expression, not multiple statements in braces.