方法 'bloc' 没有为类型 'BuildContext' 定义

The method 'bloc' isn't defined for the type 'BuildContext'

 Widget build(BuildContext context) {
    final appBar = AppBar(
      title: Text(
        "Romantic Comedy",
      ),
      actions: [
        IconButton(
          icon: Icon(Icons.search),
          onPressed: () {
            showSearch(
                context: context,
                delegate: MaterialSearch(_pagingController.itemList));
          },
        ),
      ],
    );

    return Scaffold(
        backgroundColor: Colors.black,
        appBar: appBar,
        body: BlocConsumer<MovieBloc, MovieState>(
            listener: (context, movieState) {

            },
            builder: (context, movieState) {
              if (movieState is MovieSuccessState) {
                movieBloc.movies.addAll(movieState.movies);
              }
              return GridView.builder(
                  controller: _scrollController
                    ..addListener(() {
                      if (_scrollController.offset ==
                          _scrollController.position.maxScrollExtent) {
                        context.bloc<MovieBloc>()
                          ..isFetching = true
                          ..add(Fetch());
                      }
                    }),
                  padding: EdgeInsets.only(left: 12.0, right: 12.0),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3),
                  itemBuilder: (context, index) => MovieTile(_movies[index]));
            }));
  }

我认为您使用的以下代码意味着将使用 BlocProvider 定义的父小部件中的 bloc。但是因为您似乎在此小部件中有一个 bloc 实例,所以您只需使用该实例即可。

// with extensions
context.bloc<BlocA>();

// without extensions
BlocProvider.of<BlocA>(context)

因为你在MovieSuccessState状态下使用了movieBloc, 我认为您可以使用 movieBloc 而不是 context.bloc<MovieBloc>().

来参考 isFetchingadd 方法
return GridView.builder(
                  controller: _scrollController
                    ..addListener(() {
                      if (_scrollController.offset ==
                          _scrollController.position.maxScrollExtent) {
                        //context.bloc<MovieBloc>()
                        //  ..isFetching = true
                        //  ..add(Fetch());
                        movieBloc.isFetching = true;
                        movieBloc.add(Fetch());
                      }
                    }),

在 v6.1.0 之后,context.bloccontext.repository 都被弃用,取而代之的是 context.readcontext.watch。查看 migration guide 了解更多信息。