Accessing value from a Text Field in Flutter

Using Text Field widget an app can interact with user input. So how do we get the value from the widget ?

There are two ways, one is use change event to store values to a local final variable using SetState of the widget and the second is try using Editing-Controller, which can also offer service like clearing box etc. I go with controller.

Controller

Create a text editing controller and attach it to the widget

final mycontroller = TextEditingController();

attach the controller to the widget and it will look like the following

  child: TextField(
                decoration: InputDecoration(hintText: 'Enter todo here'),
 controller: mycontroller,
                onChanged: (value) {},
              ),

Using the controller

You can use the controller to access the data and clear the data as follows

OutlineButton(
              onPressed: () {                
                var newtodo = new Todo(mycontroller.text);               
                text.clear();
})

Flutter Projects

Flutter-Kitchen-Cart


A firebase – Flutter sample application for iOS and Android

Flutter Project Planner


A MongoDB-Flutter cloud application for project management

Hivedb-Flutter Todo


This project store todo items locally using NoSQL database, Hivedb, which is written in dart. Damn blazingly fast

1 thought on “Accessing value from a Text Field in Flutter

  1. Pingback: Flutter : Showing errorText programmatically in a StatefullWidget | Manoj's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.