我的库特没有用冷冻散发出新的状态?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些在进行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.