我的库特没有用冷冻散发出新的状态?

发布于 2025-01-20 01:33:59 字数 2096 浏览 3 评论 0原文

我正在尝试使用 Cubit 进行状态管理,并且我有一个带有 @freezed 注释的状态类。我以前用过肘节,以前没有看到任何类似的问题。在 cubit 构造函数中,我使用超级构造函数发出初始状态,并且 BLocBuilder 捕获该初始状态。然后,每当我发出新的状态时,我的肘实际上就没有发出。我的代码如下所示:

promotion_state.dart

part of 'promotion_cubit.dart';

@freezed
abstract class PromotionState with _$PromotionState {
  const factory PromotionState.initial() = _Initial;
  const factory PromotionState.loaded({required TrophyIconType trophyIconType}) = _Loaded;
}

promotion_cubit.dart


Future<void> whenMenuPageDisplayed() async {
  emit(const PromotionState.loaded(trophyIconType: TrophyIconType.withAnimation));
}

menu_page.dart

return BlocProvider<PromotionCubit>(
      create: (_) => getIt<PromotionCubit>()..init(),
      child: BlocBuilder<PromotionCubit, PromotionState>(
        builder: (context, state) => state.maybeWhen(
          initial: () => const SizedBox.shrink(),
          loaded: (trophyIconType) {
            print(state);
            return _TrophyIcon(trophyIconType);
          },
          orElse: () => const SizedBox.shrink(),
        ),
      ),
    );

我在哪里调用 whenMenuPageDisplayed function:


@injectable
class MyNavigatorObserver extends NavigatorObserver {
  MyNavigatorObserver(this._promotionCubit);

  final PromotionCubit _promotionCubit;

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    if (route.settings.name == '/menu') {
      _promotionCubit.whenMenuPageDisplayed();
    }
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    if (previousRoute!.settings.name == '/menu') {
      _promotionCubit.whenMenuPageDisplayed();
    }
  }
}

print(state); 处于加载状态时不在终端中显示任何输出。

I am trying to use Cubit for state management and I have a state class with @freezed annotation. I've used cubits before and did not see any issues like that before. In cubit constructor I emit initial state with super constructor and BLocBuilder catches that initial state. Then, whenever I emit a new state, my cubit is actually not emitting. Here is how my code looks like:

promotion_state.dart

part of 'promotion_cubit.dart';

@freezed
abstract class PromotionState with _$PromotionState {
  const factory PromotionState.initial() = _Initial;
  const factory PromotionState.loaded({required TrophyIconType trophyIconType}) = _Loaded;
}

promotion_cubit.dart


Future<void> whenMenuPageDisplayed() async {
  emit(const PromotionState.loaded(trophyIconType: TrophyIconType.withAnimation));
}

menu_page.dart

return BlocProvider<PromotionCubit>(
      create: (_) => getIt<PromotionCubit>()..init(),
      child: BlocBuilder<PromotionCubit, PromotionState>(
        builder: (context, state) => state.maybeWhen(
          initial: () => const SizedBox.shrink(),
          loaded: (trophyIconType) {
            print(state);
            return _TrophyIcon(trophyIconType);
          },
          orElse: () => const SizedBox.shrink(),
        ),
      ),
    );

Where I am calling whenMenuPageDisplayed function:


@injectable
class MyNavigatorObserver extends NavigatorObserver {
  MyNavigatorObserver(this._promotionCubit);

  final PromotionCubit _promotionCubit;

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    if (route.settings.name == '/menu') {
      _promotionCubit.whenMenuPageDisplayed();
    }
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    if (previousRoute!.settings.name == '/menu') {
      _promotionCubit.whenMenuPageDisplayed();
    }
  }
}

print(state); in Loaded state is not displaying any output in the terminal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2025-01-27 01:33:59

对于那些在进行Google搜索时偶然发现这个问题的人,问题是Getit在要求时正在创建新的Cubit实例。

解决方案是用@singleton或@lazysingleton在作者案例中注释Cubit类。

For those who stumbled upon this question while doing a google search, the problem was that getIt was creating new instances of the cubit when it was requested.

The solution was to annotate the cubit class with @singleton or @lazySingleton in the authors case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文