skip to Main Content

I’m doing a meal-recipe App in which I am wrapping in a BlocBuilder as below:

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text("The Recipe Bloc"),
        backgroundColor: Colors.orange[800],
      ),
      body: Center(child: BlocBuilder<RecipeCubit, RecipeState>(
        builder: (context, state) {
          if (state is RecipeInitial) {
            return const CircularProgressIndicator(color: Colors.orange);
          }
          if (state is RecipeLoaded) {
            return state.recipeList.isEmpty
                ? const Center(child: Text("No Recipes Registered Yet!"))
                : ListView.builder(
                    itemCount: state.recipeList.length,
                    itemBuilder: (context, index) {
                      return RecipeListTile(parentContext: context, recipeItem: state.recipeList[index]).display;
                    });
          } else {
            // In Case The App Reads from Outside of Standard States
            return const Text("Something Went Wrong!");
          }
        },
      )),

      //Add New Recipes
      floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.orange[800],
          onPressed: () {
            List<Recipe> outofContext_recipeList =context.select((value) => null)
            showDialog(context: context, builder: (BuildContext context) => CreateRecipeDialog(lastID: state.recipeList.isNotEmpty ? state.recipeList.last.id : "-1"));
          },
          child: const Icon(Icons.add_rounded)),
    );
  }
}

regardless of options I put in the screen, I want to have a FloatingActionButton() for adding new recipes.

So here comes my question, How can I access my CubitState/state outside of BlocBuilder?

2

Answers


  1. Chosen as BEST ANSWER

    I've already found an answer in which I save CubitState as a state of my StatefulWidget and use it outside of BlocBuilder:

    class _HomeScreenState extends State<HomeScreen> {
      late List<Recipe> recipeCubitState;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: const Text("The Recipe Bloc"),
            backgroundColor: Colors.orange[800],
          ),
          body: Center(child: BlocBuilder<RecipeCubit, RecipeState>(
            builder: (context, state) {
              if (state is RecipeInitial) {
                return const CircularProgressIndicator(color: Colors.orange);
              }
              if (state is RecipeLoaded) {
                recipeCubitState = state.recipes;
                return state.recipes.isEmpty
                    ? const Center(child: Text("No Recipes Registered Yet!"))
                    : ListView.builder(
                        itemCount: state.recipes.length,
                        itemBuilder: (context, index) {
                          return RecipeListTile(parentContext: context, recipeItem: state.recipes[index]).display;
                        });
              } else {
                // In Case The App Reads from Outside of Standard States
                return const Text("Something Went Wrong!");
              }
            },
          )),
    
          //Add New Recipes
          floatingActionButton: FloatingActionButton(
              backgroundColor: Colors.orange[800],
              onPressed: () {
                showDialog(context: context, builder: (BuildContext context) => CreateRecipeDialog(lastID: recipeCubitState.isNotEmpty ? recipeCubitState.last.id : "-1"));
              },
              child: const Icon(Icons.add_rounded)),
        );
      }
    }
    

    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    Does anybody has a better idea?


  2. Hey to get the state of your Recipe Cubit you can use

    context.read<RecipeCubit>().state 
    

    or

    BlocProvider.of<RecipeCubit>(context).state
    

    anyone can be used as per your preference to get the state of your Recipe Cubit outside the BlocBuilder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search