initstate中的BlocProvider从blocbuilder接收值

时间:2020-04-05 13:50:10

标签: flutter

我很好奇如何在内部调用BlocProvider来从BlocBuilder检索值。 通常在小部件内部,我调用Blocbuilder来检索字符串,然后可以将字符串值传递给Bloc提供程序,以便运行获取数据。

我尝试如下所示:

  void initState(){
super.initState();
BlocBuilder<idoutletbloc,String>(
    builder: (context,idoutlet)=>BlocProvider.of<tablelistbloc>(context).add(idoutlet);
);}

,但是它说“返回无效是不是小部件”,由匿名闭包定义。 我如何从idoutletbloc检索值,然后添加BlocProvider.of(context).add(idoutlet)??

我在任何地方都找不到

这是我的集团代码

这是我的集团字符串值

class idoutletbloc extends Bloc<String, String>{
@override
String get initialState => '';
@override
Stream<String> mapEventToState(String idoutlet) async* {
yield idoutlet.toString();
}
}

我的集团获取需要接收值的数据

class viewtableactivebloc extends Bloc<String, List<ViewTableActives>>{
@override
List<ViewTableActives> get initialState => [];
@override
Stream<List<ViewTableActives>> mapEventToState(String event) async*{
List<ViewTableActives> viewtableactive =[];
try{
final response = await http.post(BaseUrl.ViewTableActive,
body: {
        "ID_Outlet": event,
      });
  final data = jsonDecode(response.body);
  if (data.length != 0) {
    print("----------START Print View Table Active----------");
    data.forEach((api) {
      viewtableactive.add(
        ViewTableActives(
          api['ID_Transactions'],
          api['ID_Customer'],
        )
      );
      print("Print View Table Actibe : "
          "ID_Transactions : "+api['ID_Transactions']+", "
          "ID_Customer : "+api['ID_Customer']+", "
      );
    });
    print("----------END Print View Table Active----------");
    print("viewtableactivebloc : sukses");
  } else {
    viewtableactive =[];
    print('data kosong');
  }

}
catch(e){
  print("Error ViewTableActive :");
  print(e);
}
yield viewtableactive;
}}

1 个答案:

答案 0 :(得分:0)

您可以像这样访问集团的当前状态:

BlocProvider.of(context).state;

或通过速记

context.bloc()。state;