diff --git a/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart b/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart index 566f56ae..31e73a8f 100644 --- a/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart +++ b/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart @@ -187,16 +187,15 @@ class _ProfilePageState extends ConsumerState { super.initState(); // 首屏数据:用户基本信息(从本地存储优先) _checkLocalAvatarSync(); - _loadUserData(); + _loadUserData().then((_) { + // 用户数据加载完成后,检查钱包状态 + _checkAndStartWalletPolling(); + }); _loadAppInfo(); // 通知数量(轻量级请求) _loadUnreadNotificationCount(); // 启动定时刷新(可见区域的数据) _startAutoRefreshTimer(); - // 检查钱包状态并启动轮询(如果需要) - WidgetsBinding.instance.addPostFrameCallback((_) { - _checkAndStartWalletPolling(); - }); } /// 加载应用信息 @@ -796,9 +795,43 @@ class _ProfilePageState extends ConsumerState { Future _checkAndStartWalletPolling() async { final authState = ref.read(authProvider); - // 如果账号已创建但钱包未就绪,启动轮询 - if (authState.isAccountCreated && !authState.isWalletReady) { - debugPrint('[ProfilePage] Account created but wallet not ready, starting polling'); + // 如果本地已标记钱包就绪,无需检查 + if (authState.isWalletReady) { + debugPrint('[ProfilePage] Wallet already ready (local), skip polling'); + return; + } + + // 如果账号已创建,先调用 API 检查钱包状态 + if (authState.isAccountCreated && _serialNumber.isNotEmpty && _serialNumber != '--') { + debugPrint('[ProfilePage] Checking wallet status from API...'); + try { + final accountService = ref.read(accountServiceProvider); + final walletInfo = await accountService.getWalletInfo(_serialNumber); + + if (walletInfo.isReady) { + // 钱包已就绪,更新本地存储 + debugPrint('[ProfilePage] Wallet is ready, updating local storage'); + final secureStorage = ref.read(secureStorageProvider); + await secureStorage.write(key: StorageKeys.isWalletReady, value: 'true'); + + // 刷新 authProvider 状态 + await ref.read(authProvider.notifier).checkAuthStatus(); + + // 通知 walletStatusProvider 更新状态 + ref.read(walletStatusProvider.notifier).stopPolling(); + + // 强制刷新UI + if (mounted) { + setState(() {}); + } + return; + } + } catch (e) { + debugPrint('[ProfilePage] Failed to check wallet status: $e'); + } + + // 钱包未就绪,启动轮询 + debugPrint('[ProfilePage] Wallet not ready, starting polling'); await ref.read(walletStatusProvider.notifier).startPolling(); } }