---
name: riverpod-cancel-fd6768bf1036
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.


---
Source attribution: dangdungvn
Original project: https://github.com/dangdungvn/review_system_app/blob/f397d2abe0a8d4c041cd274f0ffc5680d85e7c44/.github/skills/riverpod-cancel/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2024 Minh Nguyen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: dangdungvn
Original project: https://github.com/dangdungvn/review_system_app/blob/f397d2abe0a8d4c041cd274f0ffc5680d85e7c44/.github/skills/riverpod-cancel/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering
Roles: Frontend Developer, Full-Stack Developer, Mobile Developer
Specialties: Cross Platform, Frontend
Tools: Tool not specified
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: f76b011a06a0e849847c625b4167b7f33e5847f7e2e730c95be0e1ce4576b8f3
