0
0
Fluttermobile~20 mins

TextField and TextEditingController in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TextField Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you type in this TextField?
Consider this Flutter widget code snippet. What will be the value of the TextEditingController's text after typing "hello" in the TextField?
Flutter
final controller = TextEditingController();

TextField(
  controller: controller,
  decoration: InputDecoration(labelText: 'Enter text'),
);
AThe controller.text will remain empty even after typing.
BThe controller.text will be "hello" after typing.
CThe controller.text will contain "Enter text".
DThe controller.text will be null.
Attempts:
2 left
💡 Hint
The controller keeps the current text inside the TextField.
lifecycle
intermediate
1:30remaining
When should you dispose a TextEditingController?
In a StatefulWidget, when is the correct time to call dispose() on a TextEditingController?
AAfter every build() call.
BIn the widget's initState() method.
CIn the widget's dispose() method.
DYou never need to dispose it.
Attempts:
2 left
💡 Hint
Think about cleaning up resources when the widget is removed.
📝 Syntax
advanced
1:30remaining
What is the output of this code snippet?
What will be printed when this Flutter code runs and the TextField is empty, then the button is pressed?
Flutter
final controller = TextEditingController();

ElevatedButton(
  onPressed: () {
    print(controller.text.isEmpty ? 'Empty' : controller.text);
  },
  child: Text('Check'),
);
AEmpty
BPrints a blank line
CThrows a runtime error
Dnull
Attempts:
2 left
💡 Hint
Check what controller.text returns when no input is given.
🔧 Debug
advanced
2:00remaining
Why does this TextField not update the controller text?
Given this code, why does typing in the TextField not change the controller's text?
Flutter
final controller = TextEditingController(text: 'Start');

TextField(
  onChanged: (value) {
    controller.text = value;
  },
  controller: controller,
);
ABecause setting controller.text inside onChanged causes an infinite loop and blocks updates.
BBecause controller.text is read-only and cannot be set.
CBecause onChanged is never called when controller is set.
DBecause the controller must be recreated on every build.
Attempts:
2 left
💡 Hint
Think about what happens when you set controller.text inside onChanged.
🧠 Conceptual
expert
2:30remaining
How to programmatically move cursor to end of text in TextField?
You want to set the cursor position at the end of the current text in a TextField controlled by a TextEditingController. Which code snippet achieves this?
Acontroller.setCursor(offset: controller.text.length);
Bcontroller.moveCursorToEnd();
Ccontroller.cursorPosition = controller.text.length;
Dcontroller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));
Attempts:
2 left
💡 Hint
Look for how to set selection using TextSelection and TextPosition.