From cb3c7623dc68e1308db5b4d24afdf0ddd7bf1b3e Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 14 Jan 2026 08:49:52 -0800 Subject: [PATCH] fix(mining-app): fix Riverpod ref usage in router redirect callback Use cached auth state from AuthNotifier instead of ref.read() to avoid "Cannot use ref functions after provider changed" exception during rebuild. Co-Authored-By: Claude Opus 4.5 --- frontend/mining-app/lib/core/router/app_router.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/mining-app/lib/core/router/app_router.dart b/frontend/mining-app/lib/core/router/app_router.dart index 9f5e13e0..7f736409 100644 --- a/frontend/mining-app/lib/core/router/app_router.dart +++ b/frontend/mining-app/lib/core/router/app_router.dart @@ -31,7 +31,8 @@ class AuthNotifier extends ChangeNotifier { void updateLoginState(bool isLoggedIn) { if (_isLoggedIn != isLoggedIn) { _isLoggedIn = isLoggedIn; - notifyListeners(); + // 使用 Future.microtask 延迟通知,避免在 provider rebuild 期间触发 + Future.microtask(() => notifyListeners()); } } } @@ -54,7 +55,8 @@ final appRouterProvider = Provider((ref) { initialLocation: Routes.splash, refreshListenable: authNotifier, redirect: (context, state) { - final isLoggedIn = ref.read(isLoggedInProvider); + // 使用 authNotifier 中缓存的状态,而不是 ref.read + final isLoggedIn = authNotifier.isLoggedIn; final currentPath = state.uri.path; // splash 页面不做重定向,让它自己处理初始化逻辑