Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

errors when clearing TextField flutter

Tags:

flutter

I'm trying to clear my TextField, here is what i do

IconButton(
      onPressed: (){
        FocusScope.of(context).requestFocus(new FocusNode());
        _searchFieldController.clear();
        setState(() {
          searchClicked = false;
        });
      },
      icon: Icon(Icons.close),
    );
  }

when i run it i get this error

I/flutter ( 4547): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter ( 4547): The following assertion was thrown while handling a gesture: I/flutter ( 4547): invalid text selection: TextSelection(baseOffset: 6, extentOffset: 6, affinity: I/flutter ( 4547): TextAffinity.upstream, isDirectional: false)

and here is my the widget

TextField(
                  onChanged: (text) {
                    if(text.length >= 4){
                      searchResult = productTemp.where((i) => i.productName.contains(text.toString())).toList();
                      _productController.sink.add(searchResult);
                    }else{
                      _productController.sink.add(productTemp);
                    }
                  },
                  focusNode: _focus,
                  controller: _searchFieldController,
                  style: TextStyle(fontSize: 15),
                  decoration: InputDecoration(
                      filled: true,
                      hintStyle:new TextStyle(color: Colors.grey[800],fontSize: 20),
                      fillColor: Colors.white70,
                      hintText: "Search",
                      suffixIcon: searchClicked  ? buildCancelIcon() : buildSearchIcon()
                  ),
                )
like image 394
Boby Avatar asked Jul 16 '19 14:07

Boby


People also ask

How do you show error in Textinput in Flutter?

Material design allows a text field to indicate an error via a small red label under the input box: https://material.io/components/text-fields (see screenshot below).

How do you validate a text Form field in Flutter?

To validate the user input, we need the Form widget. The Form widget serves as a container to validate multiple TextFormFields and needs a GlobalKey to uniquely identify the form. It is also used to get the current state of the form.

How do you get rid of Underfield TextField Flutter?

To remove TextField underline/border in Flutter, you can simply set the border property to InputBorder. none. This will remove the underline for all states such as Focused, Enabled, Error, Disabled.


1 Answers

https://github.com/flutter/flutter/issues/17647

WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());
like image 82
crazecoder Avatar answered Nov 05 '22 19:11

crazecoder