1

TextField controlled controller.addListener(() gets called multiple time after pressing the clear button, this will only happen if we are clearing it.

Snippet:

TextEditingController controller = new TextEditingController();
    TextField field = new TextField(
      controller: controller,
      autofocus: true,

    );
    controller.addListener(() {
      print("Pressed cancel button");
    });

Video Link

Note: While adding characters in TextField listener method gets called only ones.

3
  • so what did you expect ? addListener only called once, so there are no duplication in API or database? Commented Aug 22, 2019 at 9:32
  • @ejabu: You got it. Commented Aug 22, 2019 at 10:25
  • I have the same problem with Nexus 6p when used with API level 23, but this problem did not occure with Pixel with API28 Commented Nov 30, 2019 at 4:00

3 Answers 3

3

I guess that would be a defect on flutter, a possible solution would be to use onChanged()

TextField field = new TextField(
  autofocus: true,
  onChanged: (String value) {
    print("Pressed clear button");
  },

);
Sign up to request clarification or add additional context in comments.

Comments

0

I have the same problem with Nexus 6p when used with API level 23 and Pixel with API 25.

but this problem did not occurs with Pixel with API28 and it does not occurs with Nexus6P with API26.

exact code from https://flutter.dev/docs/cookbook/forms/text-field-changes was used.

Comments

-1

1. We need to create our own .clear() method

void clearField() {
  print("c: clearField");
  var newValue = textController.value.copyWith(
    text: '',
    selection: TextSelection.collapsed(offset: 0),
  );
  textController.value = newValue;
  callApi('');
}

// and call it by : 

child: TextField(
  controller: textController,
  autofocus: true,
  decoration: InputDecoration(
    suffixIcon: IconButton(
      icon: Icon(Icons.close),
      onPressed: clearField, // call
    ),
  ),
),

2. We need to carefully handle changes

void changesOnField() {
  print("c: changesOnField");
  String text = textController.text;
  if (text.isNotEmpty) { // set this
    callApi(text);
  }
}

Full Code

You may look into this repo and build it locally Github

Result

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.