From 2e2791de5b3e8a3d38fe670fb043f553000d8ba7 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Sun, 10 Mar 2024 12:20:43 +0100 Subject: [PATCH] Improve error message fixes #2425 --- packages/riverpod/lib/src/core/ref.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/riverpod/lib/src/core/ref.dart b/packages/riverpod/lib/src/core/ref.dart index 7a8f8958f..d813c75fd 100644 --- a/packages/riverpod/lib/src/core/ref.dart +++ b/packages/riverpod/lib/src/core/ref.dart @@ -13,9 +13,14 @@ extension $RefArg on Ref { class UnmountedRefException implements Exception { @override String toString() { - return 'Cannot use a Ref after it has been disposed. ' - ' This typically happens when a provider rebuilt, but the previous "build" was still pending and is still performing operations.' - ' It is generally fine to let this exception be thrown, as it will be caught and handled by Riverpod.'; + return ''' +Cannot use a Ref after it has been disposed. This typically happens if: +- A provider rebuilt, but the previous "build" was still pending and is still performing operations. + You should therefore either use `ref.onDispose` to cancel pending work, or + check `ref.mounted` after async gaps or anything that could invalidate the provider. +- You tried to use Ref inside `onDispose` or other life-cycles. + This is not supported, as the provider is already being disposed. +'''; } }