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

套件詳情v1.2.0

cancelation_token

此套件提供取消權杖的介面,以及各種實作(可取消、逾時、組合等)。

AndroidiOSLinuxmacOSWebWindows
近 30 天下載
4.8萬
喜歡數
2
Pub 分數
160

安裝套件

flutter pub add cancelation_token

套件介紹

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

cancelation_token 套件提供了一個乾淨的介面來管理非同步作業中的取消,支援多種實作方式,例如可取消的任務、逾時及組合式權杖。它讓開發者能跨平台優雅地處理作業取消,提升 Flutter 應用的回應性與資源管理。適用於網路請求、計時器或可能需要中斷的長時間運算等情境。此套件輕量且專為搭配 Dart 的 async/await 結構設計。

適用情境

  • 管理可取消的網路請求
  • 實作逾時機制
  • 協調多個非同步操作
  • 處理使用者啟動的任務取消
  • 控制長時間執行的背景流程

主要特色

  • 對可取消的未來物件的支援
  • 基於超時的取消
  • 複合令牌分組
  • 跨平台相容性
  • 簡單的 API 結合

適合對象

  • 需要對非同步操作進行細粒度控制的開發者
  • 頻繁使用網路或定時器的應用
  • 需要取消支援的狀態管理系統
  • 重視乾淨異步程式碼結構的團隊

使用建議

  • 需要理解 async/await 流程
  • 對於簡單使用案例,可能會增加複雜性
  • 不適用於非異步操作
  • 除了基本範例之外,文件內容有限
  • 最佳搭配其他狀態管理解決方案使用

原始 README

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

cancelation_token

This package provides an interface for cancelation tokens as well as various implementations:

  • CancelableToken which can be canceled programmatically;
  • CompositeToken which is canceled when any or all tokens are canceled;
  • TimeoutToken which is canceled after a timeout period.

Example

Cancel a long-running execution after a timeout.

import 'package:cancelation_token/cancelation_token.dart';

void main() async {
  final timeout = Duration(seconds: 5);
  final errorOnTimeout = CanceledException.withStackTrace('Too long!');
  final token = TimeoutToken(timeout, errorOnTimeout);
  token.ensureStarted();

  try {
    await longProcess(token);
  } on CanceledException catch (ex, st) {
    print(ex.message);
    print(ex.stackTrace);
    print(st);
  }
}

Future<void> longProcess(CancelationToken token) async {
  // very long indeed!
  while (true) {
    token.throwIfCanceled();
    await Future.delayed(const Duration(milliseconds: 100));
  }
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單