简体   繁体   English

使用不包含 Bloc 类型的上下文调用 BlocProvider.of()

[英]BlocProvider.of() called with a context that does not contain a Bloc of type

@override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
    providers: [
         BlocProvider<TripDetailBloc>(create: (BuildContext context) => TripDetailBloc()),
         BlocProvider<PopUpBloc>(create: (BuildContext context) => PopUpBloc()),
               ],
    child: Scaffold(
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          BlocProvider.of<TripDetailBloc>(context).add(AddTripDetailPannelEvent());
        },
      ),
      appBar: appbar(),
      body: pannel(),
    )
    );
  }

The following assertion was thrown while handling a gesture:处理手势时抛出以下断言:

  • BlocProvider.of() called with a context that does not contain a Bloc of type TripDetailBloc.使用不包含 TripDetailBloc 类型的 Bloc 的上下文调用 BlocProvider.of()。
  • No ancestor could be found starting from the context that was passed to BlocProvider.of <TripDetailBloc>() .从传递给 BlocProvider.of <TripDetailBloc>()的上下文开始找不到祖先。
  • This can happen if the context you used comes from a widget above the BlocProvider.如果您使用的上下文来自 BlocProvider 上方的小部件,则可能会发生这种情况。
  • he context used was: TripDetailPage(dependencies: [MediaQuery], state: _TripDetailPageState#d4ab3)他使用的上下文是:TripDetailPage(依赖项:[MediaQuery],state:_TripDetailPageState#d4ab3)

Change your code to this:将您的代码更改为:

Widget build(BuildContext context) {
  
  return MultiBlocProvider(
      providers: [
        BlocProvider<TripDetailBloc>(create: (BuildContext context) => TripDetailBloc()),
        BlocProvider<PopUpBloc>(create: (BuildContext context) => PopUpBloc()),
      ],
      child: Builder(
        builder: (context) {
          return Scaffold(
            floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                BlocProvider.of<TripDetailBloc>(context).add(AddTripDetailPannelEvent());
              },
            ),
            appBar: appbar(),
            body: pannel(),
          );
        }
      )
  );
}

If you look closely, I have rapped your Scaffold into a widget builder.如果您仔细观察,我已将您的 Scaffold 敲击成一个小部件构建器。

Wrap your scaffold in a builder widget and use that context.将脚手架包装在构建器小部件中并使用该上下文。 The context the.of(context) is using is the same of the method build(BuildContext context), that's why it doesn't find it the.of(context) 使用的上下文与方法 build(BuildContext context) 相同,这就是它找不到它的原因

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 使用不包含 Bloc 类型的上下文调用的 BLoC BlocProvider.of() - Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 TrackingBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc 使用不包含 FicheMvtBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type FicheMvtBloc BlocProvider.of() 使用不包含 OfflineBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type OfflineBloc BlocProvider.of() 调用的上下文不包含 WeatherBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type WeatherBloc Flutter:使用不包含 Bloc 类型的上下文调用 blocprovider.of() - Flutter: blocprovider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 CLASS 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type CLASS 使用不包含 Bloc 类型的上下文调用 Flutter BlocProvider.of() - Flutter BlocProvider.of() called with a context that does not contain a Bloc of type 使用不包含 TaharatBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type TaharatBloc BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM