In this recipe, we’ll see how to retrieve the text a user has typed into a textfield.

Directions

  • Create a TextEditingController
  • Supply the TextEditingController to a TextField
  • Display the current value of the text field

1. Create a TextEditingController

In order to retrieve the text a user has typed into a text field, we need tocreate aTextEditingController.We will then supply the TextEditingController to a TextField in the nextsteps.

Once a TextEditingController is supplied to a TextField or TextFormField,we can use it to retrieve the text a user has typed into that text field.

Note: It is also important to dispose of the TextEditingController when weare finished using it. This will ensure we discard any resources used by theobject.

  1. // Define a Custom Form Widget
  2. class MyCustomForm extends StatefulWidget {
  3. @override
  4. _MyCustomFormState createState() => _MyCustomFormState();
  5. }
  6. // Define a corresponding State class. This class will hold the data related to
  7. // our Form.
  8. class _MyCustomFormState extends State<MyCustomForm> {
  9. // Create a text controller. We will use it to retrieve the current value
  10. // of the TextField!
  11. final myController = TextEditingController();
  12. @override
  13. void dispose() {
  14. // Clean up the controller when the Widget is disposed
  15. myController.dispose();
  16. super.dispose();
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. // We will fill this out in the next step!
  21. }
  22. }

2. Supply the TextEditingController to a TextField

Now that we have a TextEditingController to work with, we need to wire it upto a specific text field. To do this, we’ll supply the TextEditingControllerto a TextField or TextFormField Widget as the controller property.

  1. TextField(
  2. controller: myController,
  3. );

3. Display the current value of the text field

After we’ve supplied the TextEditingController to our text field, we can beginreading values! We will use thetextmethod provided by the TextEditingController to retrieve the String of textthe user has typed into the text field.

In this example, we will display an alert dialog with the current value ofthe text field when the user taps on a floating action button.

  1. FloatingActionButton(
  2. // When the user presses the button, show an alert dialog with the
  3. // text the user has typed into our text field.
  4. onPressed: () {
  5. return showDialog(
  6. context: context,
  7. builder: (context) {
  8. return AlertDialog(
  9. // Retrieve the text the user has typed in using our
  10. // TextEditingController
  11. content: Text(myController.text),
  12. );
  13. },
  14. );
  15. },
  16. tooltip: 'Show me the value!',
  17. child: Icon(Icons.text_fields),
  18. );

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Retrieve Text Input',
  8. home: MyCustomForm(),
  9. );
  10. }
  11. }
  12. // Define a Custom Form Widget
  13. class MyCustomForm extends StatefulWidget {
  14. @override
  15. _MyCustomFormState createState() => _MyCustomFormState();
  16. }
  17. // Define a corresponding State class. This class will hold the data related to
  18. // our Form.
  19. class _MyCustomFormState extends State<MyCustomForm> {
  20. // Create a text controller. We will use it to retrieve the current value
  21. // of the TextField!
  22. final myController = TextEditingController();
  23. @override
  24. void dispose() {
  25. // Clean up the controller when the Widget is disposed
  26. myController.dispose();
  27. super.dispose();
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Scaffold(
  32. appBar: AppBar(
  33. title: Text('Retrieve Text Input'),
  34. ),
  35. body: Padding(
  36. padding: const EdgeInsets.all(16.0),
  37. child: TextField(
  38. controller: myController,
  39. ),
  40. ),
  41. floatingActionButton: FloatingActionButton(
  42. // When the user presses the button, show an alert dialog with the
  43. // text the user has typed into our text field.
  44. onPressed: () {
  45. return showDialog(
  46. context: context,
  47. builder: (context) {
  48. return AlertDialog(
  49. // Retrieve the text the user has typed in using our
  50. // TextEditingController
  51. content: Text(myController.text),
  52. );
  53. },
  54. );
  55. },
  56. tooltip: 'Show me the value!',
  57. child: Icon(Icons.text_fields),
  58. ),
  59. );
  60. }
  61. }

Retrieve Text Input Demo