13
 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]));
            }));
  }
3
  • 4
    What version of flutter_bloc you are using? after v6.1.0 both context.bloc and context.repository are deprecated in favor of context.read and context.watch checkout the migration guide Jul 7, 2021 at 4:49
  • 1
    flutter_bloc: ^7.0.1 Jul 7, 2021 at 5:40
  • 1
    Ok @Adnan alshami you can give this as an answer and I'll mark it as the right answer. Jul 8, 2021 at 5:11

2 Answers 2

31

After flutter_bloc v6.1.0,

Both context.bloc and context.repository are deprecated and replaced with context.read and context.watch.

Checkout the migration guide for more information.

1
  • perfect solution for me , big thanks
    – Apps Maker
    Mar 23, 2022 at 0:37
1

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());
                      }
                    }),
2
  • Sorry , I need to access bloc from parent Jul 7, 2021 at 5:39
  • Through 'movieBloc.movies.addAll(movieState.movies);', I think that there is a bloc instance in this widget already. With your bloc version and Adnan Alshami's comment, it seems that you need to migrate.
    – KuKu
    Jul 7, 2021 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.