1

In my screen there are search features as per logic that's working fine but issue is I want to run my search function after stope typing?? So in the TextFormField there are onChanged method, how can we achieve it? I tried lots of ways like comparing DateTime but not able to achieved it.

~ PS : onEditingComplete method works perfect in my case but issues is, To call this method I have to click on return button, without click on return button is it possible?

Govaadiyo
  • 5,644
  • 9
  • 43
  • 72
  • 1
    please reference this https://stackoverflow.com/questions/51791501/how-to-debounce-textfield-onchange-in-dart – chunhunghan Oct 20 '20 at 09:02

1 Answers1

2

I would suggest to create a debouncer, something like this.

class Debouncer {
  final int milliseconds;
  VoidCallback action;
  Timer _timer;

  Debouncer({this.milliseconds});

  run(VoidCallback action) {
    if (null != _timer) {
      _timer.cancel();
    }
    _timer = Timer(Duration(milliseconds: milliseconds), action);
  }
}

Then initiate the object with your desired time of "after stop typing".

final _debouncer = Debouncer(milliseconds: 1000);

And use it in your onChanged method.

onChanged: (string) {
  _debouncer.run(() {
    //Perform your search
  }
);
Tom Langer
  • 36
  • 4