fix(client): 切换账号显示上一账号数据——登录/登出按会话纪元重建 ProviderScope

张三登录后看到汤宇飞的订单列表(含已删行):业务 provider 的 notifier 实例
与 _cache 兜底缓存跨账号存活,拉取失败时把上一账号数据端给新账号(10 个
列表 provider 同款模式)。根治:会话纪元 sessionEpoch(Riverpod 体系外
ValueNotifier)作 ProviderScope 的 key,login/logout 后自增重建整棵状态树。
logout 的撤销会话 POST 改为不阻塞,确保清盘先于 scope 重建,防止新 scope
restore 读到旧 token 把上一账号登回来。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-15 00:39:18 +08:00
parent 858f24f74b
commit b8567b0f4e
3 changed files with 42 additions and 11 deletions
+12
View File
@@ -0,0 +1,12 @@
import 'package:flutter/foundation.dart';
/// 会话纪元:每次显式登录 / 登出成功后自增(见 AuthNotifier.login/logout)。
///
/// main.dart 以其为 ProviderScope 的 key —— 纪元变化即整棵 Riverpod 状态树销毁重建,
/// 所有业务 provider(含 notifier 实例字段里的分页 / 筛选 / _cache 兜底缓存)随账号
/// 切换彻底清零,杜绝跨账号内存残留(如订单列表显示上一账号数据的泄漏)。
///
/// 必须在 Riverpod 体系外(scope 重建时它自身要存活),故用裸 ValueNotifier。
/// 只在 login()/logout() 主动 bump;启动恢复持久化会话(restore())不 bump
/// 否则「重建 → restore → 又 bump」死循环。
final ValueNotifier<int> sessionEpoch = ValueNotifier(0);