How to use multi functions in the cubit block

25 Views Asked by At

I am building a homepage that has 2 different items from API, I am using one cubit to handle 2 API's with 2 functions as follows:

  List<SubCatModal> mySubCat = [];
  List<SubCatModal> subCat() {
    print("asd");
    emit(HomeLoaded());
    homeRepository.getSubCatRepository().then((respone) {
      emit(HomeSubCatSuccessCase(subCatList: respone));
      mySubCat = respone;
    });
    return mySubCat;
  }

  List<SubCatModal> myFirstSlider = [];
  List<SubCatModal> SliderListCubit() {
    print("asdwww");

    emit(HomeLoaded());

    homeRepository.getFirstSliderRepository().then((respone) {
      emit(HomeFirstSliderSuccessCase(firstSliderList: respone));
      myFirstSlider = respone;
    });
    return myFirstSlider;
  } 

And I set a bloc provider to the route as follows:

case Routes.homeScreen:
        return MaterialPageRoute(
          builder: (_) => BlocProvider(
            create: (BuildContext context) =>
                HomeCubit(HomeRepository(homeApi: HomeApi())),
            child: const Homepage(),
          ),
        );

And on the screen, I call the cubit like this:

  void initState() {
    super.initState();
    BlocProvider.of<HomeCubit>(context).subCat();
    BlocProvider.of<HomeCubit>(context).SliderListCubit();
  }

I use blocbulider to show the list:

BlocBuilder<HomeCubit, HomeState>(
          builder: (context, state) {
          if (state is HomeSubCatSuccessCase) {return ""}

Everything works fine for the first function in the cubit (subCat), but the other one does not work at all, so how can I use different function in one screen and one cubit to fetch API?

0

There are 0 best solutions below