How to debounce Textfield onChange in Dart?

后端 未结 9 1216
攒了一身酷
攒了一身酷 2020-12-03 04:49

I\'m trying to develop a TextField that update the data on a Firestore database when they change. It seems to work but I need to prevent the onChange event to fire multiple

9条回答
  •  被撕碎了的回忆
    2020-12-03 05:09

    As others have suggested, implementing a custom debouncer class is not that difficult. You can also use a Flutter plugin, such as EasyDebounce.

    In your case you'd use it like this:

    import 'package:easy_debounce/easy_debounce.dart';
    
    ...
    
    // Start listening to changes 
    nomeTextController.addListener(((){
        EasyDebounce.debounce(
            '_updatenomecliente',        // <-- An ID for this debounce operation
            Duration(milliseconds: 500), // <-- Adjust Duration to fit your needs
            () => _updateNomeCliente()
        ); 
    }));
    

    Full disclosure: I'm the author of EasyDebounce.

提交回复
热议问题