The method 'bloc' isn't defined for the type 'BuildContext'
2,671
Solution 1
After v6.1.0, both context.bloc
and context.repository
are deprecated in favor of context.read
and context.watch
. checkout the migration guide for more information.
Solution 2
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());
}
}),
Comments
-
Abraham Mathew over 1 year
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])); })); }
-
Adnan Alshami almost 3 yearsWhat version of
flutter_bloc
you are using? afterv6.1.0
bothcontext.bloc
andcontext.repository
are deprecated in favor ofcontext.read
andcontext.watch
checkout the migration guide -
Abraham Mathew almost 3 yearsflutter_bloc: ^7.0.1
-
Abraham Mathew almost 3 yearsOk @Adnan alshami you can give this as an answer and I'll mark it as the right answer.
-
-
Abraham Mathew almost 3 yearsSorry , I need to access bloc from parent
-
KuKu almost 3 yearsThrough '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.
-
Apps Maker about 2 yearsperfect solution for me , big thanks
-
Adnan Alshami about 2 yearsyou're welcome @AppsMaker