Fix ProviderDisposedException

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Cannot use the Ref of totalChapterCacheSizeStateProvider 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.

#0      Ref._throwIfInvalidUsage (package:riverpod/src/core/ref.dart:220:7)
ref.dart:220
#1      AnyNotifier.state= (package:riverpod/src/core/provider/notifier_provider.dart:91:9)
notifier_provider.dart:91
#2      TotalChapterCacheSizeState.build.<anonymous closure> (package:mangayomi/modules/more/data_and_storage/providers/storage_usage.dart:17:42)
storage_usage.dart:17
#3      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:948:45)
future_impl.dart:948
#4      Future._propagateToListeners (dart:async/future_impl.dart:977:13)
future_impl.dart:977
#5      Future._completeWithValue (dart:async/future_impl.dart:720:5)
future_impl.dart:720
<asynchronous suspension>
This commit is contained in:
NBA2K1 2025-11-29 01:31:22 +01:00
parent 5fdbf530cb
commit 33152fc035
2 changed files with 6 additions and 1 deletions

View file

@ -119,6 +119,8 @@ class _MyAppState extends ConsumerState<MyApp> {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (ref.read(clearChapterCacheOnAppLaunchStateProvider)) {
// Watch before calling clearcache to keep it alive, so that _getTotalDiskSpace completes safely
ref.watch(totalChapterCacheSizeStateProvider);
ref
.read(totalChapterCacheSizeStateProvider.notifier)
.clearCache(showToast: false);

View file

@ -14,7 +14,10 @@ part 'storage_usage.g.dart';
class TotalChapterCacheSizeState extends _$TotalChapterCacheSizeState {
@override
String build() {
_getTotalDiskSpace().then((value) => state = _formatBytes(value));
_getTotalDiskSpace().then((value) {
if (!ref.mounted) return;
state = _formatBytes(value);
});
return "0.00 B";
}