Skip to content

An easy-to-use flutter package that provides debounce and throttle with Stream and WidgetBuilder.

License

Notifications You must be signed in to change notification settings

seosh817/easy_debounce_throttle

Repository files navigation

Easy Debounce Throttle

Platform Pub Package Build Status License: MIT


An easy-to-use flutter package that provides debounce and throttle with Stream and WidgetBuilder.

Usage

If you want to subscribe to debounce, throttle events with Stream, use EasyDebounce, EasyThrottle

you should call the close() method to avoid memory leak

EasyDebounce

 final EasyDebounce _easyDebounce = EasyDebounce(delay: const Duration(milliseconds: 1000));
 final TextEditingController _textController = TextEditingController();
 
 @override
 void initState() {
 super.initState();
 _textController.addListener(() {
   _easyDebounce.debounce(_textController.text);
 });
 _easyDebounce.listen((data) {
   setState(() {
     _debounceText = '$data';
   });
 });
 }
 
 @override
 void dispose() {
 _easyDebounce.close();
 super.dispose();
}

EasyThrottle

 final EasyThrottle _easyThrottle = EasyThrottle(delay: const Duration(milliseconds: 1000));
 final TextEditingController _textController = TextEditingController();
 
 @override
 void initState() {
 super.initState();
 _textController.addListener(() {
   _easyThrottle.throttle(_textController.text);
 });
 _easyThrottle.listen((data) {
   setState(() {
     _throttleText = '$data';
   });
 });
 }
 
 @override
 void dispose() {
 _easyThrottle.close();
 super.dispose();
 }

If you want to receive debounce and throttle events from methods such as onPressed callbacks with WidgetBuilder, use EasyDebounceBuilder and EasyThrottleBuilder.

EasyDebounceBuilder and EasyThrottleBuilder internally disposes the stream, so there is no need to call the close() method.

EasyDebounceBuilder

EasyDebounceBuilder(
    delay: const Duration(milliseconds: 1000),
    builder: (context, debounce) {
      return TextButton(
          onPressed: () => debounce(() => _increaseDebounceCount()),
       // onPressed: () {
       //   debounce(() {
       //     _increaseDebounceCount();
       //   });
       // },
          child: Text(
            'increase debounce count',
            style: kNotoSansBold14.copyWith(color: Colors.white),
          ));
    }),

EasyThrottleBuilder

EasyThrottleBuilder(
    delay: const Duration(milliseconds: 1000),
    builder: (context, throttle) {
      return TextButton(
          onPressed: () => throttle(() => _increaseThrottleCount()),
       // onPressed: () {
       //   throttle(() {
       //     _increaseThrottleCount();
       //   });
       // },
          child: Text(
            'increase throttle count',
            style: kNotoSansBold14.copyWith(color