title: Flutter BLoC: How to add a new event after Navigator.push without wrapping MaterialApp with BlocProvider
body: <p>So I have screen <code>ExploreScreen</code> which initiates my BLoC <strong>FilteredRecipesBloc</strong>.
  Inside this screen, there is a button that navigates to a new screen <code>FilterScreen</code>.
  From the <code>FilterScreen</code>,  I want to add a new event which affects both screens. The problem now is that I'm getting this error message (<strong>onError Bad state: Cannot add new events after calling close</strong>). Is this possible without wrapping MaterialApp with a BlocProvider? I just want local bloc access to two screens.<p>
  ExploreScreen:</p>
  
  <pre class="lang-dart prettyprint-override"><code>class ExploreScreen extends StatefulWidget {
    @override
    _ExploreScreenState createState() =&gt; _ExploreScreenState();
  }
  
  class _ExploreScreenState extends State&lt;ExploreScreen&gt; {
    FilteredRecipesBloc _filteredRecipesBloc;
  
    @override
    void initState() {
      _filteredRecipesBloc = FilteredRecipesBloc(
          recipeList:
              (BlocProvider.of&lt;RecipesBloc&gt;(context).state as RecipesLoaded)
                  .recipeList);
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Explore"),
        ),
        body: BlocProvider(
          create: (context) =&gt; _filteredRecipesBloc,
          child: BlocBuilder&lt;FilteredRecipesBloc, FilteredRecipesState&gt;(
              builder: (context, state) {
            if (state is FilteredRecipeEmpty) {
              return CategoriesScreen();
            }
            if (state is FilteredRecipesLoading) {
              return Column(
                children: &lt;Widget&gt;[
                  CircularProgressIndicator(),
                  IconButton(
                    icon: Icon(Icons.add),
                    onPressed: () {
                      _filteredRecipesBloc.add(UpdateRecipesFilter(
                          ingredients: ["Curry"], maxCookTime: 30));
                    },
                  ),
                ],
              );
            }
            if (state is FilteredRecipeLoaded) {
              return ListView.builder(
                  itemCount: state.recipeList.length,
                  itemBuilder: (_, int index) {
                    return ImageRecipeContainer(recipe: state.recipeList[index]);
                  });
            }
            return Container();
          }),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _navigateToFilterScreen,
          child: Icon(EvaIcons.funnelOutline),
          heroTag: "fafdsf",
        ),
      );
    }
  
    void _navigateToFilterScreen() {
      Navigator.of(context)
          .push(MaterialPageRoute&lt;FilterScreen&gt;(builder: (context) {
        return BlocProvider.value(
          value: _filteredRecipesBloc,
          child: FilterScreen(_filteredRecipesBloc),
        );
      }));
    }
  
    @override
    void dispose() {
      _filteredRecipesBloc.close();
      super.dispose();
    }
  }
  </code></pre>
  
  <p>Filter Screen:</p>
  
  <pre class="lang-dart prettyprint-override"><code>class FilterScreen extends StatefulWidget {
    final FilteredRecipesBloc filteredRecipesBloc;
  
    FilterScreen(this.filteredRecipesBloc);
    @override
    _FilterScreenState createState() =&gt; _FilterScreenState();
  }
  
  class _FilterScreenState extends State&lt;FilterScreen&gt; {
    Map&lt;String, bool&gt; _selectedCategories = {};
    Map&lt;String, bool&gt; _selectedIngredients = {};
  
    @override
    void initState() {
      _initIngredientList();
      super.initState();
    }
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("Filter"),),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: &lt;Widget&gt;[
                Text("Category",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),),
                ChoiceChip(
                    label: Text("Vegan"),
                  selected: _selectedCategories["vegan"] == true,
                  onSelected: (isActive){
                      setState(() {
                        _selectedCategories["vegan"] = isActive;
                      });
                  },
                ),
                Text("Ingredients"),
                ShowMoreChoiceChips(children: _buildIngredientChoiceChips()),
                RaisedButton(onPressed: _updateFilter),
  
  
              ],
            ),
          ),
        ),
      );
    }
  
    void _initIngredientList(){
      List&lt;Recipe&gt; recipeList =
          (BlocProvider.of&lt;RecipesBloc&gt;(context).state as RecipesLoaded).recipeList ?? [];
  
      for(int i = 0; i &lt; recipeList.length;i++){
        for(int y = 0; y &lt; recipeList[i].ingredientsFlat.length;y++){
          _selectedIngredients[recipeList[i].ingredientsFlat[y].name] = false;
        }
      }
    }
  
    List&lt;Widget&gt; _buildIngredientChoiceChips(){
      List&lt;Widget&gt; widgetList = [];
      _selectedIngredients.forEach((key, value){
        widgetList.add(ChoiceChip(label: Text(key), selected: value,onSelected: (isActive){
          setState(() {
            _selectedIngredients[key] = isActive;
          });
        },));
      });
      return widgetList;
    }
  
    void _updateFilter(){
      List&lt;String&gt; ingredients = [];
      _selectedIngredients.forEach((k,v){
        if(v) ingredients.add(k);
      });
  
      widget.filteredRecipesBloc.add(
          UpdateRecipesFilter(ingredients: ingredients.isNotEmpty ? ingredients : null));
      //BlocProvider.of&lt;FilteredRecipesBloc&gt;(context).add(
        //  UpdateRecipesFilter(ingredients: ingredients.isNotEmpty ? ingredients : null),);
    }
  }
  </code></pre>