Issue with HomeBloc Creation Halting App on Homepage due to Shared Preferences Dependent Use Cases

35 Views Asked by At

Upon attempting to initialize the HomeBloc using Injectable, which depends on specific use cases (storeTokenLocallyUseCase and retrieveTokenLocallyUseCase), I encountered a roadblock. These use cases are dependent on Shared Preferences, and their injection into HomeBloc is causing the application to halt during its initialization on the homepage.

@module
abstract class DatasourceModule {
  @singleton
  Future<SharedPreferences> get provideSharedPreferences async =>
      await SharedPreferences.getInstance();
}

@module
abstract class UseCaseModule {
  @lazySingleton
  StoreTokenLocallyUseCase provideStoreTokenLocallyUseCase(
      SharedPreferences preferences) {
    return StoreTokenLocallyUseCaseImpl(preferences);
  }

  @lazySingleton
  RetrieveTokenLocallyUseCase provideRetrieveTokenLocallyUseCase(
      SharedPreferences preferences) {
    return RetrieveTokenLocallyUseCaseImpl(preferences);
  }
}

@Environment(PlatformType.mobile)
@injectable
class HomeBloc extends SideEffectBloc<HomeEvent, HomeState, HomeEffect> {
  final StoreTokenLocallyUseCase storeTokenLocallyUseCase;
  final RetrieveTokenLocallyUseCase retrieveTokenLocallyUseCase;
  var logger = TraceLogger();

  HomeBloc(this.storeTokenLocallyUseCase, this.retrieveTokenLocallyUseCase)
      : super(const HomeState(title: "My state of data")) {
    on<InitialData>((event, emit) {
      logger.i("HomeBloc - InitialData");
      emit(state.copyWith(title: "Click"));
      produceSideEffect(HomeEffect.displayError("Test error"));
    });
    on<OnButtonClicked>((event, emit) {});
  }
}

I expected the dependencies relying on Shared Preferences to be injected into HomeBloc seamlessly, enabling the app to continue functioning as expected without any interruption.

0

There are 0 best solutions below