0

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.

shuster
  • 184
  • 3
  • 10
  • Can you add the code where you call the `whenMenuPageDisplayed` function? – Advait Apr 08 '22 at 18:30
  • I 've added the function. – shuster Apr 08 '22 at 18:58
  • check if the function is being called with a true if statement by adding a print statement inside the if block. – Advait Apr 08 '22 at 19:04
  • It is actually called bc other functionalities in whenMenuPageDisplayed func. are working (not in the example). – shuster Apr 08 '22 at 19:14
  • 1
    With this design, there might be a chance that you might be injecting two different cubits to `MyNavigatorObserver` and the `BlocProvider`. Can you confirm that the cubit is annotated with @singleton – Advait Apr 08 '22 at 19:19
  • It is weird. It was annotedted with `injectable` and I am trying to make it `singleton`. get_it gives me an error in this case. I'll find the issue. – shuster Apr 10 '22 at 21:48
  • What's the error you're getting? – Advait Apr 12 '22 at 07:50
  • 1
    I've used `@lazySingleton` annotation and it works, thanks. – shuster Apr 13 '22 at 16:09

1 Answers1

2

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.

Advait
  • 574
  • 3
  • 12