You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Equatable导致Flutter页面无法重复跳转问题的修复咨询

修复连续点击Home按钮无法跳转的问题

问题根源

你的所有LandingState子类(HomeState、LoginState等)继承自Equatable但未重写props,默认返回空数组。Bloc内部会通过Equatable的相等性判断决定是否通知监听者:当关闭HomePage后再次点击Home按钮,Bloc emit的新HomeState和之前的旧状态被判定为相等,因此不会触发BlocListener的回调,页面跳转逻辑也就不会执行。

修复方案

方案一:给每个State添加唯一区分属性

为每个状态类添加唯一标识(比如时间戳),确保每次emit的状态实例都是不相等的:

@immutable
abstract class LandingState extends Equatable {
  const LandingState();
}

class LandingInitial extends LandingState {
  @override
  List<Object> get props => [];
}

class LoginState extends LandingState {
  final DateTime timestamp;

  const LoginState() : timestamp = DateTime.now();

  @override
  List<Object> get props => [timestamp];
}

class HomeState extends LandingState {
  final DateTime timestamp;

  const HomeState() : timestamp = DateTime.now();

  @override
  List<Object> get props => [timestamp];
}

方案二:强制跳过状态相等性检查

在Bloc的emit方法中传入emitWhenNotEqual: false,强制触发状态更新,忽略相等性判断:

class LandingBloc extends Bloc<LandingEvent, LandingState> {
  LandingBloc() : super(LandingInitial()) {
    on<LandingEvent>((event, emit) {
      if (event is HomeClick) {
        emit(HomeState(), emitWhenNotEqual: false);
      } else if (event is LoginClick) {
        emit(LoginState(), emitWhenNotEqual: false);
      }
    });
  }
}

方案选择

  • 方案一更符合Bloc的状态管理设计原则,通过状态本身的差异性驱动更新,适合大多数场景。
  • 方案二更简洁直接,适合简单的页面跳转场景,但频繁触发无差异状态更新可能带来不必要的组件重建。

内容的提问来源于stack exchange,提问作者K K Muhammed Fazil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:55:17