title: Flutter - Access Bloc from parent on children
body: <p>I have a parent widget that has two Blocs:</p>
  <pre><code>...
  @override
    Widget build(BuildContext context) {
      return MultiBlocProvider(
        providers: [
          BlocProvider&lt;RestaurantBloc&gt;(
            create: (BuildContext context) =&gt; RestaurantBloc(restaurantRepository: _restaurantRepository),
          ),
          BlocProvider&lt;CartBloc&gt;(
            create: (BuildContext context) =&gt; CartBloc(),
          ),
        ],
        child: RestaurantScreenWidget(),
      );
    }
  ...
  </code></pre>
  <p>Section of the parent widget where child widgets get generated using the <code>CartBloc</code> and other Bloc:</p>
  <pre><code>...
  BlocBuilder&lt;RestaurantBloc, RestaurantState&gt;(
    builder: (context, state) {
      if (state is RestaurantEmpty) {
        return Center(
          child: Text('Empty'),
        );
      }
  
      if (state is RestaurantLoaded) {
        final items = state.restaurantCategories;
  
        if (items.length &gt;= 1) {
          return BlocBuilder&lt;CartBloc, CartState&gt;(
            builder: (context, cartState) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: &lt;CategoryList&gt;[
                  for (var item in items)
                    CategoryList(categoryName: item.name, categoryItems: item.items)
                ],
              ); 
            },
          );
        }
  
        return Center(
          child: Text('Empty restaurant'),
        );
      }
  
      return Center(
        child: Text(
          'Error'
        ),
      );
    }
  ),
  ...
  </code></pre>
  <p>And then it generates various child widgets. Those widgets need access to <code>CartBloc</code> and its state:</p>
  <pre><code>...
  Padding(
    padding: EdgeInsets.only(top: 20),
    child: SizedBox(
      width: double.infinity,
      child: BlocBuilder&lt;CartBloc, CartState&gt;(
        builder: (context, cartState) {
          return RaisedButton(
            child: const Text('ADD TO BASKET'),
              color: Colors.blue,
               textColor: Colors.white,
               onPressed: () {
                 BlocProvider.of&lt;CartBloc&gt;(context).add(
                   AddItemToCart(
                     itemCount: (state as ItemLoaded).amount,
                     itemPrice: args.itemPrice,
                     newItemId: args.itemId,
                     newItemRestrictions: (state as ItemLoaded).restrictions,
                     newItemName: args.itemName
                   )
                 );
                 Navigator.pop(context);
               },
             );
           } 
         ), 
       ),
     ),
     ...
  </code></pre>
  <p>But by doing it like this, It gives the error:</p>
  <p><a href="https://i.stack.imgur.com/NSKE5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NSKE5.png" alt="enter image description here" /></a></p>
  <p>However, if I put a <code>MultiBlocProvider</code> with the <code>CartBloc</code> on the child widgets, the error goes away, but obviously the parent widget doesn't shares the state of the child widgets.</p>
  <p>How can I handle the state that needs to be share between these two widgets? (The generate child widgets push to a screen that needs access to <code>CartBloc</code> and its state).</p>