Bloc: BlocProvider can not be used in children's children

Created on 4 Sep 2019  路  3Comments  路  Source: felangel/bloc

BlocProvider.of<BlocA>(context)
Its a common way to get a bloc instance of parent's BlocA.

But if i want to use in this way.

 BlocProvider<BlocA>(
      child: Child1(),
      builder: (context){
        return _blocAInstance;
      },
    )

class Child1 extends statelesswidget{
Container(child: Child2());
}

class Child2 extends statelesswidget{
@override
build(context){
var _blocAInstance = BlocProvider.of<BlocA>(context);
}
}

This will throw Exception.

Is there anyway to let Child2 get the top BlocA instance?

question

Most helpful comment

@felangel Thanks for reply. I found a bug in my code, BlocProvider.of works fine . Thanks.

All 3 comments

Hi @Liuzhiyang94 馃憢
Thanks for opening an issue!

This should work just fine. Can you please provide a link to a sample project which illustrates the problem you鈥檙e having? Thanks 馃憤

Closing for now but feel free to comment with a sample app and I'm more than happy to help 馃憤

@felangel Thanks for reply. I found a bug in my code, BlocProvider.of works fine . Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

ricktotec picture ricktotec  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

timtraversy picture timtraversy  路  3Comments

clicksocial picture clicksocial  路  3Comments