简体   繁体   中英

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]));
            }));
  }

在 v6.1.0 之后,不推荐使用context.bloccontext.repository以支持context.readcontext.watch检查迁移指南

I think that the below code that you used means that will use bloc from the parent widget defined with BlocProvider . But because it seems that you have a bloc instance in this widget you just use that instance.

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

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

Because you used movieBloc at the MovieSuccessState state, I think you can just refer isFetching and add method using movieBloc instead of context.bloc<MovieBloc>() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM