Riverpod request cancellation and debouncing

VerifiedSafe

Learn to cancel Riverpod async requests with ref.onDispose and debounce rapid refreshes. Perfect for cleanup when a provider is disposed.

Sby Skills Guide Bot
DevelopmentIntermediate
8/4/2026
Claude CodeCursorWindsurfCopilotCodex
#riverpod#flutter#dart#async#state-management

Recommended for

Automated summary

Explains how to cancel or debounce Riverpod async requests using ref.onDispose together with auto-dispose providers.

Strengths

  • Shows the concrete ref.onDispose(client.close) pattern for cancelling an in-flight request
  • Clearly separates the two scenarios: cancelling on page exit and debouncing rapid refreshes
  • Details the didDispose flag with Future.delayed trick to abort a refresh triggered too quickly
  • Suggests a reusable extension on Ref to centralise debounce and cancellation logic

Limitations

  • Code snippets stay schematic; the exact API depends on the HTTP package you use
  • Defers to the official Riverpod how-to for a complete extension example
  • Assumes prior familiarity with auto-dispose and ref.watch
When to use it

Reach for it when a Riverpod provider must cancel a request as the user leaves the page or avoid repeated calls during rapid refreshes.

When not to use it

Skip it if you are not using Riverpod, or if you need a generic cancellation approach independent of provider lifecycle.

Security analysis

Safe
Quality score78/100

The skill provides guidance on Riverpod provider lifecycle and debouncing patterns in Dart/Flutter. It does not instruct destructive actions, exfiltration, obfuscation, or use of powerful tools. No executable commands or network operations are invoked by the skill itself.

No concerns found

How this analysis is made

Examples

Cancel request on page exit
My Flutter provider keeps fetching after the user navigates away. How do I cancel the in-flight request when the provider is disposed with Riverpod?
Debounce rapid refreshes
Users spam the refresh button and we fire a request every tap. Show me how to debounce Riverpod refreshes with ref.onDispose and a delay.
Reusable debounced client extension
Write a Ref extension in Riverpod that debounces requests and cancels them on dispose so I can reuse it across providers.

name: riverpod-cancel description: Cancel or debounce Riverpod async requests with ref.onDispose; cancel when user leaves the page, debounce rapid refreshes. Use when the user asks about cancelling requests, debouncing, or cleaning up when a provider is disposed.

Riverpod — Cancelling and debouncing requests

Instructions

Use ref.onDispose together with auto-dispose (or ref.watch) to cancel in-flight requests when the user leaves the page, or to debounce rapid refreshes.

Cancelling when leaving the page

Ensure the provider is auto-dispose. When the user navigates away, the provider loses its listeners and is disposed; ref.onDispose runs. In that callback, cancel the request (e.g. close the HTTP client used for the request).

Example pattern: create an http.Client() in the provider, pass it to your fetch logic, and call ref.onDispose(client.close). When the provider is disposed, the client closes and the request is cancelled. Exact API depends on your HTTP package.

Debouncing refreshes

If the user triggers refresh multiple times quickly, you can delay the actual request until they stop (e.g. 500ms). Pattern:

  1. In the provider, set a flag (e.g. didDispose = false) and ref.onDispose(() => didDispose = true).
  2. await Future.delayed(duration).
  3. If didDispose is true, the user triggered another refresh during the delay; throw to abort (Riverpod catches and ignores). Otherwise create the client, register onDispose(client.close), and run the request.

So: delay first, then create client and run request; disposal during the delay aborts, disposal after creates client cancels the request.

Reusable extension

You can implement an extension on Ref that returns a debounced/cancellable client:

  • Use onDispose to set a "cancelled" flag.
  • Wait for the debounce duration; if disposed, throw.
  • Create the client, onDispose(client.close), return the client.

Use this extension in your activity/provider so all call sites get debounce + cancel behavior. See the official Riverpod "cancel" how-to for a full extension example.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/riverpod-request-cancellation-and-debouncing-mse7invy/download' --output '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md.part' && mv '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md.part' '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && test -s '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/riverpod-cancel' && cp -R '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy' '.claude/skills/riverpod-cancel'

Cursor
test -f '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && test -s '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/riverpod-cancel' && cp -R '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy' '.cursor/skills/riverpod-cancel'

Codex
test -f '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && test -s '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/riverpod-cancel' && cp -R '.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy' '.agents/skills/riverpod-cancel'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy'

openskills read 'riverpod-request-cancellation-and-debouncing-mse7invy'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/riverpod-request-cancellation-and-debouncing-mse7invy/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills