【问题标题】:BlocProvider inside initstate that receive value from blocbuilder从 blocbuilder 接收值的 initstate 中的 BlocProvider
【发布时间】:2020-07-17 11:56:58
【问题描述】:

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

我尝试如下所示:

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

但它说“返回是 void 不是 Widget”,正如匿名闭包所定义的那样。 我如何从 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;
}}

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

    BlocProvider.of(context).state;

    或通过简写

    context.bloc().state;

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2021-10-25
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      相关资源
      最近更新 更多