Himanshu Ranjan
Himanshu Ranjan

Reputation: 302

Why TextEditingController is not working in flutter?

I am trying to implemenent a browser type search bar and using a text field to write a url there:

 TextEditingController _textcontroller;
  Widget searchbar() {
    return Container(
      child: Row(
        children: [
          Container(
            width: 230,
            child: TextField(
              controller: _textcontroller,
            ),
          ),
          GestureDetector(
            child: Icon(Icons.search),
            onTap: () {
              setState(() {
                print(_textcontroller.text);
                url = _textcontroller.text;
              });
            },
          )
        ],
      ),
    );
  }

I am using this widget in the appbar to place it in place of the title but everytime I press the search button the text returns null.

Scaffold(
      appBar: AppBar(
        title: searchbar(),
        actions: [
          NavigationControls(_controller.future),
        ],

I am getting this error: The getter 'text' was called on null. Receiver: null Tried calling: text

Can someone help me with issue?

Upvotes: 1

Views: 4509

Answers (1)

Payam Asefi
Payam Asefi

Reputation: 2757

Change this:

TextEditingController _textcontroller;

To this:

TextEditingController _textcontroller=TextEditingController();

Upvotes: 3

Related Questions