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

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(),
    )
    );
  }

处理手势时抛出以下断言:

将您的脚手架包裹在构建器小部件中并使用该上下文。 .of(context) 使用的上下文与方法 build(BuildContext context) 相同,这就是它找不到它的原因

将您的代码更改为:

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(),
          );
        }
      )
  );
}

如果你仔细看,我已经把你的脚手架变成了一个小部件生成器。