Flutter 使用不包含类型为的BLoC的上下文调用的Flatter BLoC BlocProvider.of()
与之类似,到目前为止还没有相关的解决方案 我得到以下错误:Flutter 使用不包含类型为的BLoC的上下文调用的Flatter BLoC BlocProvider.of(),flutter,bloc,flutter-bloc,Flutter,Bloc,Flutter Bloc,与之类似,到目前为止还没有相关的解决方案 我得到以下错误: BlocProvider.of() called with a context that does not contain a Bloc of type CategoryBloc. class _TestWidgetState extends State<TestWidget> { @override Widget build(BuildContext context) { return Scaffold(
BlocProvider.of() called with a context that does not contain a Bloc of type CategoryBloc.
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Test')),
body: BlocProvider(
builder: (context) => sl<CategoryBloc>(), // dependency injection via GetIt
child: Column(
children: [
BlocBuilder<CategoryBloc, CategoryState>(builder: (context, state) {
if (state is Empty) {
return Text('Empty');
}
if (state is Loading) {
return Text('Loading');
}
if (state is Loaded) {
return Text('Loaded');
}
}),
RaisedButton(
child: Text('Press me'),
onPressed: () {
dispatchAction(context);
},
),
],
),
),
);
}
void dispatchAction(BuildContext context) {
BlocProvider.of<CategoryBloc>(context).dispatch(GetAllCategories());
}
}
唯一的小部件:
BlocProvider.of() called with a context that does not contain a Bloc of type CategoryBloc.
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Test')),
body: BlocProvider(
builder: (context) => sl<CategoryBloc>(), // dependency injection via GetIt
child: Column(
children: [
BlocBuilder<CategoryBloc, CategoryState>(builder: (context, state) {
if (state is Empty) {
return Text('Empty');
}
if (state is Loading) {
return Text('Loading');
}
if (state is Loaded) {
return Text('Loaded');
}
}),
RaisedButton(
child: Text('Press me'),
onPressed: () {
dispatchAction(context);
},
),
],
),
),
);
}
void dispatchAction(BuildContext context) {
BlocProvider.of<CategoryBloc>(context).dispatch(GetAllCategories());
}
}
为完整起见,我将提供所有集团文件
类别bloc
class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
final GetAllCategoriesUsecase getAllCategories;
CategoryBloc({@required this.getAllCategories})
: assert(getAllCategories != null);
@override
CategoryState get initialState => Empty();
@override
Stream<CategoryState> mapEventToState(CategoryEvent event) async* {
if (event is GetAllCategories) {
yield Loading();
final failureOrCategories =
await getAllCategories(Params(limit: 10, skip: 0));
yield failureOrCategories.fold(
(failure) => Error(error: _mapFailureToMessage(failure)),
(categories) => Loaded(list: categories));
}
}
String _mapFailureToMessage(Failure failure) {
...
}
}
到目前为止,我已经尝试:
- 指定类型
-相同错误正文:BlocProvider(
任何帮助都会受到极大的欢迎。在谷歌搜索数小时后找到了答案 Flatter_bloc要求BlocProvider和BlocBuilder位于单独的小部件中,这样bloc Builder的上下文在层次结构上“位于”Builder的上下文之下
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Test')),
body: BlocProvider<CategoryBloc>(
builder: (context) => sl<CategoryBloc>(), //
child: TestWidget2(),
),
);
}
}
class TestWidget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
BlocBuilder<CategoryBloc, CategoryState>(builder: (context, state) {
if (state is Empty) {
return Text('Empty');
}
if (state is Loading) {
return Text('Loading');
}
if (state is Loaded) {
return Text('Loaded');
}
}),
RaisedButton(
child: Text('Press me'),
onPressed: () {
dispatchAction(context);
},
),
],
);
}
class\u TestWidgetState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:Text('Test')),
正文:BlocProvider(
生成器:(上下文)=>sl()//
子项:TestWidget2(),
),
);
}
}
类TestWidget2扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
BlocBuilder(生成器:(上下文,状态){
如果(状态为空){
返回文本('Empty');
}
如果(状态为正在加载){
返回文本(“加载”);
}
如果(状态已加载){
返回文本(“已加载”);
}
}),
升起的按钮(
child:Text('Press me'),
已按下:(){
调度行动(上下文);
},
),
],
);
}
以下是该问题的解决方案:
get_it: ^3.0.1
flutter_bloc: ^0.21.0 # old version :(
equatable: ^1.0.1
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Test')),
body: BlocProvider<CategoryBloc>(
builder: (context) => sl<CategoryBloc>(), //
child: TestWidget2(),
),
);
}
}
class TestWidget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
BlocBuilder<CategoryBloc, CategoryState>(builder: (context, state) {
if (state is Empty) {
return Text('Empty');
}
if (state is Loading) {
return Text('Loading');
}
if (state is Loaded) {
return Text('Loaded');
}
}),
RaisedButton(
child: Text('Press me'),
onPressed: () {
dispatchAction(context);
},
),
],
);
}