搜索插件、代码仓与 APP 分类

插件详情v3.0.0

async_queue

此 Dart 包确保您的异步任务包按顺序执行,一个接一个。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
8920
喜欢数
29
Pub 分数
150

安装插件

flutter pub add async_queue

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

async_queue 包确保一系列异步任务按顺序依次执行,保持执行顺序并防止并发问题。它提供了一个简单的队列机制,用于以受控方式处理异步操作,特别适用于任务依赖关系或执行顺序至关重要的场景。该包基于 Dart 构建,可无缝集成到所有主要平台的 Flutter 项目中。该包轻量且专注于可靠性,适用于后台处理、API 调用排序,或任何需要有序异步执行的用例。

适合场景

  • 顺序 API 调用
  • 后台任务处理
  • 依赖顺序的操作
  • 带有异步操作的状态管理
  • 按顺序加载资源

主要特点

  • 保证顺序执行
  • 简单的队列接口
  • 跨平台支持
  • 轻量且依赖极少
  • 线程安全操作

适合对象

  • 需要有序异步执行的开发者
  • 具有依赖异步工作流的应用程序
  • 需要可预测任务调度的项目
  • Flutter 应用针对多个平台
  • 注重代码简洁性的团队

使用建议

  • 不适用于并行任务执行
  • 每个实例限于单个队列
  • 需要仔细管理队列生命周期
  • 最适合小型到中型任务批次
  • 不处理重试逻辑或错误恢复

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

"Buy Me A Coffee"

Async Queue - ensure your list of async tasks execute in order

This dart package ensures your pack of async tasks executes in order, one after the other.

What's new in v3.0.0

  • addJob returns a Future — await individual job results directly, no more listener workarounds
  • Automatic retry on exceptions — jobs that throw are retried up to retryTime without manual retry() calls
  • onError callback — handle errors at the queue level: AsyncQueue(onError: (error, label) { ... })
  • Priority queue — addJob(..., priority: 10) to run important jobs first
  • Pause / Resume — pause() halts without losing jobs, resume() continues
  • Retry with delay — addJob(..., retryDelay: Duration(seconds: 2)) to wait between retries
  • Job timeout — addJob(..., timeout: Duration(seconds: 30)) to auto-fail slow jobs
  • State getters — isRunning, isPaused, isClosed

Features

  • (Normal Queue) Add multiple jobs into queue before firing
  • (Auto Queue) Firing job as soon as any job is added to the queue
  • (Both) Option to add queue listener that emits events that happen in the queue
  • Retry when a job failed with optional delay between retries
  • addJob returns a Future — await individual job results
  • Automatic retry on exceptions
  • onError callback for dedicated error handling
  • Priority-based job ordering
  • Pause / Resume without losing queued jobs
  • Per-job timeout
  • isRunning / isPaused / isClosed state getters

Installing and import the library:

Like any other package, add the library to your pubspec.yaml dependencies:

dependencies:
    async_queue: <latest_version>

Then import it wherever you want to use it:

import 'package:async_queue/async_queue.dart';

Usage

1. Normal Queue
 final asyncQ = AsyncQueue();
  asyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 1), () => print("normalQ: 1")));
  asyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 4), () => print("normalQ: 2")));
  asyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 2), () => print("normalQ: 3")));
  asyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 1), () => print("normalQ: 4")));

  await asyncQ.start();

    // normalQ: 1
    // normalQ: 2
    // normalQ: 3
    // normalQ: 4
2. Auto Start Queue
final autoAsyncQ = AsyncQueue.autoStart();

  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 1), () => print("AutoQ: 1")));
  await Future.delayed(const Duration(seconds: 6));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 0), () => print("AutoQ: 1.2")));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 0), () => print("AutoQ: 1.3")));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 4), () => print("AutoQ: 2")));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 3), () => print("AutoQ: 2.2")));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 2), () => print("AutoQ: 3")));
  autoAsyncQ.addJob((_) =>
      Future.delayed(const Duration(seconds: 1), () => print("AutoQ: 4")));

    // AutoQ: 1
    // AutoQ: 1.2
    // AutoQ: 1.3
    // AutoQ: 2
    // AutoQ: 2.2
    // AutoQ: 3
    // AutoQ: 4
Add Queue Listener
  final asyncQ = AsyncQueue();

  asyncQ.addQueueListener((event) => print("$event"));
Await job result (v3.0.0+)

addJob returns a Future that completes with the job's return value. This works alongside previousResult — they serve different purposes.

final q = AsyncQueue.autoStart();

// Each addJob returns a Future you can await for its result
final userFuture = q.addJob((_) async {
  return await api.getUser();  // returns "Sam"
});

// previousResult still chains between jobs
final postsFuture = q.addJob((previousResult) async {
  return await api.getPostsFor(previousResult);
});

final user = await userFuture;     // "Sam"
final posts = await postsFuture;   // [Post, Post, ...]
Automatic retry on exceptions (v3.0.0+)

Jobs that throw are automatically retried up to retryTime times. You no longer need to catch errors and call retry() manually (though you still can for custom logic).

q.addJob((_) async {
  // if this throws, it will be retried automatically
  return await api.fetchData();
}, retryTime: 3);
onError callback (v3.0.0+)

Handle errors at the queue level without parsing events:

final q = AsyncQueue(
  onError: (error, jobLabel) {
    print('Job $jobLabel failed: $error');
  },
);
Priority (v3.0.0+)

Higher priority jobs execute before lower priority ones. Default is 0. Same-priority jobs maintain FIFO order.

q.addJob((_) => lowPriorityTask(), priority: 1);
q.addJob((_) => highPriorityTask(), priority: 10);  // runs first
Pause / Resume (v3.0.0+)

Unlike stop() which destroys the queue, pause() preserves all queued jobs:

q.pause();   // current job finishes, no new jobs start
q.resume();  // picks up where it left off
Retry with delay (v3.0.0+)

Add a delay between retry attempts:

q.addJob((_) => callApi(), retryTime: 3, retryDelay: Duration(seconds: 2));
Job timeout (v3.0.0+)

Auto-fail a job if it exceeds a duration. Timed-out jobs trigger auto-retry like any other exception:

q.addJob((_) => slowTask(), timeout: Duration(seconds: 30));
Tell queue to retry a job
    q.addJob(() async {
      try {
        //do something
      } catch (e) {
        q.retry();
      }
    },
    //default is 1
     retryTime: 3,
    );
Flutter use cases:

This package would be useful if you have multiple widgets in a screen or even in multiple screens that need to do some async requests that are related to each other.

For examples:

  • To make one request from a widget wait for another request from another widget to finish.
  • To avoid multiple requests from the front end hitting the backend in a short time, which would confuse the backend.

Code example:

 @override
  Widget build(BuildContext context) {
    final aQ = AsyncQueue.autoStart();
    return Scaffold(
      body: Column(
        children: [
          TextButton(
            onPressed: () async {aQ.addJob((_) => Future.delayed(const Duration(seconds: 2), () => print("job1 ")));},
            child: const Text('job1'),
          ),
          TextButton(
            onPressed: () async {aQ.addJob((_) => Future.delayed(const Duration(seconds: 4), () => print("jobs2")));},
            child: const Text('job2'),
          ),
          TextButton(
            onPressed: () async {aQ.addJob((_) => Future.delayed(const Duration(seconds: 1), () => print("job3")));},
            child: const Text('job3'),
          ),
        ],
      ),
    );
  }

Appreciate Your Feedbacks and Contributes

If you find anything need to be improve or want to request a feature. Please go ahead and create an issue in the Github repo

所属分类

源码仓库

使用此插件的项目

暂无相关数据。