Complete the code to create a TextField that uses a controller.
final myController = TextEditingController();
TextField(controller: [1]);The controller property of TextField needs the variable myController that holds the TextEditingController instance.
Complete the code to clear the text inside the TextEditingController.
myController.[1]();The clear() method empties the text inside the TextEditingController.
Fix the error in the code to properly dispose the TextEditingController in a StatefulWidget.
@override
void dispose() {
[1].dispose();
super.dispose();
}You must call dispose() on the controller variable (here myController) to free resources when the widget is removed.
Fill both blanks to create a TextField that updates a variable when text changes.
String text = ''; TextField( onChanged: ([1]) { [2] = value; }, );
The onChanged callback receives the new text as value. We assign it to the variable text to keep track of the input.
Fill all three blanks to create a TextEditingController, assign it to a TextField, and dispose it properly.
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { final [1] = TextEditingController(); @override void dispose() { [2].dispose(); super.dispose(); } @override Widget build(BuildContext context) { return TextField(controller: [3]); } }
The same variable myController is created, disposed, and assigned to the TextField controller.