搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.3

easy_debounce

一個極其容易使用的 Dart/Flutter 方法呼叫防抖套件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
41萬
喜歡數
438
Pub 分數
150

安裝套件

flutter pub add easy_debounce

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

easy_debounce 是一個輕量級的 Dart/Flutter 套件,可簡化方法呼叫去抖的實作。它提供直覺的 API 來延遲函數執行,避免過度呼叫——非常適合搜尋輸入、即時驗證或頻繁的使用者互動。此套件可無縫整合至 Flutter 項目中,並支援所有主要平台。透過最少的設定,開發者可提升效能並減少不必要的處理。

適用情境

  • 即時搜尋過濾
  • 表單輸入驗證
  • API 請求限流
  • 即時文字處理
  • 使用者互動最佳化

主要特色

  • 簡單的一行整合
  • 自動取消待處理呼叫
  • 跨平台支援
  • 最小化樣板程式碼
  • 乾淨、易讀的語法

適合對象

  • 尋求快速去抖解決方案的開發者
  • 頻繁更新 UI 的項目
  • 需要輸入速率限制的應用
  • 初學者學習狀態管理

使用建議

  • 不適合複雜的定時邏輯
  • 超出基本延遲之外的自訂功能有限
  • 依賴 Dart 的非同步功能
  • 可能需要對邊緣情況額外處理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

easy_debounce

An extremely easy-to-use method call debouncer, throttler, rate limiter package for Dart/Flutter.

Debouncing

Debouncing is needed when there is a possibility of multiple calls to a method being made within a short duration of each other, and it's desireable that only the last of those calls actually invoke the target method.

So basically each call starts a timer, and if another call happens before the timer executes, the timer is reset and starts waiting for the desired duration again. When the timer finally does time out, the target method is invoked.

Throttling

Throttling is sort of the opposite of debouncing - only the first call to a method actually invokes the method, and any subsequent calls that are made within a specified duration are ignored. This can be useful to throttle presses on a refresh button for example.

Rate limiting

Rate limiting is similar to throttling - the first call to a method actually invokes the method, and any subsequent calls that are made within a specified duration are recorded and the once the duration has elapsed then the last call will be executed. This can be useful to rate limit progress updates as you want the first, last and periodic updates in between.

Usage

Debouncing

Use the debouncer by calling debounce:

EasyDebounce.debounce(
    'my-debouncer',                 // <-- An ID for this particular debouncer
    Duration(milliseconds: 500),    // <-- The debounce duration
    () => myMethod()                // <-- The target method
);

The above call will invoke myMethod() after 500 ms, unless another call to debounce() with the same tag is made within 500 ms. A tag identifies this particular debouncer, which means you can have multiple different debouncers running concurrently and independent of each other.

Cancelling a debouncer

A debouncer which hasn't yet executed its target function can be called by calling cancel() with the debouncers tag:

EasyDebounce.cancel('my-debouncer');

To cancel all active debouncers, call cancelAll():

EasyDebounce.cancelAll();
Counting active debouncers

You can get the number of active debouncers (debouncers which haven't yet executed their target methods) by calling count():

print('Active debouncers: ${EasyDebounce.count()}'); 
Fire a debouncer target function manually

If you need to fire the target function of a debouncer before the timer executes, you can call fire():

EasyDebounce.fire('my-debouncer');

This will execute the debouncers target function, but the debounce timer will keep running unless you also call cancel().

Throttling

Use the throttler by calling throttle:

EasyThrottle.throttle(
    'my-throttler',                 // <-- An ID for this particular throttler
    Duration(milliseconds: 500),    // <-- The throttle duration
    () => myMethod()                // <-- The target method
    onAfter: (){ ... }              // <-- Optional callback, called after the duration has passed 
);

The above call will invoke myMethod() once, and any subsequent calls to throttle() with the same tag within 500 ms are ignored. A tag identifies this particular throttler, which means you can have multiple different throttlers running concurrently and independent of each other.

The onAfter callback will be invoked after the throttler is done, i.e. when the specified duration has passed and the throttler is no longer ignoring calls.

Cancelling a throttler

A throttler which hasn't yet executed its target function can be called by calling cancel() with the throttlers tag:

EasyThrottle.cancel('my-throttler');

To cancel all active throttlers, call cancelAll():

EasyThrottle.cancelAll();
Counting active throttlers

You can get the number of active throttlers (throttlers which are still ignoring subsequent calls to their target methods) by calling count():

print('Active throttlers: ${EasyThrottle.count()}'); 
Rate Limiting

Use the rate limiter by calling rateLimit:

EasyRateLimit.rateLimit(
    'my-ratelimiter',               // <-- An ID for this particular rate limiter
    Duration(milliseconds: 500),    // <-- The rate limiter duration
    () => myMethod()                // <-- The target method
    onAfter: (){ ... }              // <-- Optional callback, called after the duration has passed 
);

The above call will invoke myMethod() once, and any subsequent calls to rateLimit() with the same tag within 500 ms are recorded, after 500 ms has elapsed the last call will be executed until no calls have been made within the subsequent 500 ms durations. A tag identifies this particular rate limiter, which means you can have multiple different rate limiters running concurrently and independent of each other.

The onAfter callback will be invoked on duration after the rate limiter has no calls to execute.

Cancelling a rate limiter

A rate limiter which is waiting for the duration to pass can be called by calling cancel() with the rate limiter tag:

EasyRateLimit.cancel('my-ratelimiter');

To cancel all active rate limiters, call cancelAll():

EasyRateLimit.cancelAll();
Counting active rate limiter

You can get the number of active rate limiters (rate limiters which are still ignoring subsequent calls to their target methods) by calling count():

print('Active rate limiters: ${EasyRateLimit.count()}'); 

所屬分類

原始碼儲存庫

使用此套件的專案