Home > OS >  Flutter handle TextField submitted
Flutter handle TextField submitted

Time:10-22

I have a text field that I search with. I use the follwing codes:

TextField(
  controller: searchController,
  maxLines: 1,
  autofocus: true,
  onSubmitted: _search(),
),
_search(){
    print('here');
  }

But in output _serach method is called in a infinite loop when I submit text field. I want to call _search method one time.

CodePudding user response:

Well, first of all onSubmitted must be void Function (String?), while you passing void Function(). Try Something like this, it should work. At least it works for me:

TextField(
    controller: _controller,
    onSubmitted: (String value) async {
        await _performSearch(value);
    },
),
  • Related