Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Key Widgets
  • UI Components
  • Design & Animations
  • Forms & Gestures
  • Navigation & Routing
  • Flutter Interview Questions
  • Dart
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Flutter - Prefix and Suffix Icon in TextField
Next article icon

Retrieve Data From TextFields in Flutter

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we’ll learn how to retrieve data from TextFields. TextField() widget is the most common widget used in flutter apps to take user input. We’ll talk about two major methods used to extract text from TextField.

Using Variables

The TextField widget has various callback properties through which we can extract text. Majorly, onChanged is used as it takes input on every change incurred in the TextField. This callback doesn’t work when TextField’s text is changed programmatically. Basically, this kind of change is initiated by the app itself.

Syntax:

TextField(
onChanged: (value) {
print("The value entered is : $value");
}
)

Various other callbacks are also available for TextFields like onTap, onSubmitted, onEditingComplete.

  • onTap: It is called for each unique tap except for every second tap of double-tap. Internally, it builds a GestureDetector to handle this kind of event. You can use it whenever you want to trigger some property of TextField which is gesture-based.
  • onSubmitted: It is called whenever the user indicates that they are done with editing the text. Primarily, whenever the done button is pressed on the keyboard, it will be called and the data entered would be stored.
  • onEditingComplete: It is very similar to onSubmitted. The only difference is that it is called whenever the button in the bottom right corner of the keyboard is pressed. It might be ‘done’, ‘send’, ‘go’, or ‘search’.

Example:

Below is the example of onChanged in TextField. Here, we have used an anonymous function to receive a callback from onChanged. The value from callback is received in value, then we pass it to our variable title.

Dart
import "package:flutter/material.dart";  void main() => runApp(const MyApp());  class MyApp extends StatelessWidget {   const MyApp({Key? key}) : super(key: key);    @override   Widget build(BuildContext context) {     return const MaterialApp(       debugShowCheckedModeBanner: false,       home: Home(),     );   } }  class Home extends StatefulWidget {   const Home({Key? key}) : super(key: key);    @override   // ignore: library_private_types_in_public_api   _HomeState createState() => _HomeState(); }  class _HomeState extends State<Home> {   // var to store   // onChanged callback   late String title;   String text = "No Value Entered";    void _setText() {     setState(() {       text = title;     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: const Text('GeeksforGeeks'),         backgroundColor: Colors.green,         foregroundColor: Colors.white,       ),       body: Column(         children: [           Padding(             padding: const EdgeInsets.all(15),             child: TextField(               decoration: const InputDecoration(labelText: 'Title'),               onChanged: (value) => title = value,             ),           ),           const SizedBox(             height: 8,           ),           ElevatedButton(               onPressed: _setText,               style: ButtonStyle(                 elevation: WidgetStateProperty.all(8),                 backgroundColor: WidgetStateProperty.all(Colors.green),                 foregroundColor: WidgetStateProperty.all(Colors.white),               ),               child: const Text('Submit')),           const SizedBox(             height: 20,           ),           Text(text),         ],       ),     );   } } 

To know more about ElevatedButton in Flutter refer this article : Flutter – ElevatedButton Widget

Output:


Using Controller

Another way to retrieve text is by using the controller. It is a property that flutter provides with TextField. Below are the steps explaining the use of the controller.

  • First, create an object of the class TextEditingController().  It is the default class that is provided by flutter.
  • Connect the object created to the controller of TextField.
  • Now, you may create a function to get the latest value.

It works almost the same way as onChanged. But, in certain scenarios, it is preferred to use the controller as the retrieving process is managed by the flutter engine.

Example:

The below example explains using the controller for retrieving values from TextField. Firstly, create an object of the type TextEditingController. Then we assign this object to the controller property of TextField.

Dart
import "package:flutter/material.dart"; void main() => runApp(const MyApp());  class MyApp extends StatelessWidget {   const MyApp({Key? key}) : super(key: key);    @override   Widget build(BuildContext context) {     return const MaterialApp(       debugShowCheckedModeBanner: false,       home: Home(),     );   } }  class Home extends StatefulWidget {   const Home({Key? key}) : super(key: key);    @override // ignore: library_private_types_in_public_api   _HomeState createState() => _HomeState(); }  class _HomeState extends State<Home> {   final titleController = TextEditingController();   String text = "No Value Entered";    void _setText() {     setState(() {       text = titleController.text;     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: const Text('GeeksforGeeks'),         backgroundColor: Colors.green,         foregroundColor: Colors.white,       ),       body: Column(         children: [           Text(             "Using Controller",             style: TextStyle(fontSize: 30),           ),           Padding(             padding: const EdgeInsets.all(15),             child: TextField(               decoration: const InputDecoration(labelText: 'Title'),               controller: titleController,             ),           ),           const SizedBox(             height: 8,           ),           ElevatedButton(               onPressed: _setText,               style: ButtonStyle(                   elevation: WidgetStateProperty.all(8),                   backgroundColor: WidgetStateProperty.all(Colors.green),                   foregroundColor: WidgetStateProperty.all(Colors.white)),               child: const Text('Submit')),           const SizedBox(             height: 20,           ),           Text(text),         ],       ),     );   } } 


Output:


Both methods can be used for retrieving text, as the output of both is the same. Here, we had to re-run the build method to update text, hence we have used a stateful widget. If in your program, you just want to store the value, a stateless widget can also be used.



Next Article
Flutter - Prefix and Suffix Icon in TextField

T

tejinder2000
Improve
Article Tags :
  • Flutter
  • Flutter
  • Flutter Class

Similar Reads

  • Flutter - Return Data from Screen
    Interaction with UI is an essential part of any application. During the same, there might be a need to return data from the screen. This kind of interaction can range from selecting an option to navigating to different routes through the various buttons in the UI. In this article, we will explore th
    4 min read
  • Multiline TextField in Flutter
    Multiline TextField is the input TextField which takes the input in more than one line, This type of TextField is used in the scenario like taking Feedback from the user, User Comments, and Description, etc., We can achieve multiline TextField by setting the property keyBoardType and maxLines of the
    2 min read
  • Flutter - Gradient TextFields
    Decorating text fields can be a big task if you have a large application. There is a package gradient_textfield that could make this time-consuming task pretty simple and fast. Although in Flutter we can create text fields using TextField, for beginners especially, it takes time to understand decora
    3 min read
  • Retrieve the Value of the TextField in Flutter
    The text field is widely used in Android Applications for taking user input, using TextEditingController we can retrieve the data from the text field. A sample video gives you an idea of what we are going to do in this article. Step By Step ImplementationStep 1: Create a New Project in Android Studi
    2 min read
  • Flutter - Prefix and Suffix Icon in TextField
    In this article, we will implement how to add prefix and Suffix icons in the TextField. A sample image is given below to get an idea about what we are going to do in this article. Step by Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter Development on Android Stud
    2 min read
  • Flutter - Fetching Data From the Internet
    In today's world, most applications heavily rely on fetching information from the servers through the internet. In Flutter, such services are provided by the http package. In this article, we will explore this concept. Steps to Implement Data Fetching from the InternetStep 1 : Create a new flutter a
    4 min read
  • Flutter - Restrict TextField to Input Special Characters
    In Flutter, user input validation is essential for maintaining data integrity and ensuring a smooth user experience. One common requirement is to restrict the input of special characters in a TextField to prevent unwanted characters in certain fields, such as usernames or any other text-based input.
    3 min read
  • Custom Label Text in TextFormField in Flutter
    Today we will explore how to make a custom label above the text field whose style will change according to your tap on the text field/text form field. So that user can easily understand which text field is currently active. Example: In this, there are two fields, the first is email and the second is
    5 min read
  • Flutter - Extract Data From an Image to Text
    Google ML kit provides many features, one of them is Image to Text Extraction, through which we can simply extract text from an image. To extract text from the image first we need to take an image as input either from the gallery or camera for which we will use 'image_picker: ^1.0.4' (dependency fro
    3 min read
  • Persist Data with SQLite in Flutter
    SQLite is an open-source computer database, it's wont to store a piece of information, perform completely different operations like add, delete, and update. SQLite doesn't need a server or backend code; all the info is saved to a computer file within the device, or we can say it's native to storage.
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences