使用 BlocProvider 的 Bloc 插件问题
Bloc plugin issue using BlocProvider
我是 Flutter
和整个 Bloc
架构的新手,所以我正在按照教程来进一步了解它,但看起来 Bloc
在某种程度上被重新设计了,因为我的版本有完全不同的构造函数。
不知道是 Bloc
问题还是我做错了什么。
我的 BlocProvider:
BlocProvider<PlacesBloc> buildBody(BuildContext context) {
return BlocProvider(
create: (_) => sl<PlacesBloc>(),
child: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
// Top view
SizedBox(height: 20),
PlacesControls(),
// Bottom view
BlocBuilder<PlacesBloc, PlacesState>(
// ignore: missing_return
builder: (context, state) {
if (state is Empty) {
return MessageDisplay(
message: 'Search places around you!',
);
} else if (state is Loading) {
return LoadingWidget();
} else if (state is Loaded) {
return PlacesDisplay(places: state.places);
} else if (state is Error) {
return MessageDisplay(
message: state.message,
);
}
},
),
],
),
),
),
);
}
create
参数在我的 Bloc 版本中不存在。
也在我的 Widget
中,我使用 BlocProvider.of<PlacesBloc>(context).add(GetPlacesFromServer());
将事件添加到我的 'BlocProvider',但是这个 .add
方法在我的 Bloc 中不存在。
这是教程视频(底部)和我的(顶部)之间的构造函数差异:
您的教程可能是为库的 4.0.0 版编写的,它应该像您展示的那样工作。
5.0.0和6.0.0版本有很多breaking changes
我是 Flutter
和整个 Bloc
架构的新手,所以我正在按照教程来进一步了解它,但看起来 Bloc
在某种程度上被重新设计了,因为我的版本有完全不同的构造函数。
不知道是 Bloc
问题还是我做错了什么。
我的 BlocProvider:
BlocProvider<PlacesBloc> buildBody(BuildContext context) {
return BlocProvider(
create: (_) => sl<PlacesBloc>(),
child: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
// Top view
SizedBox(height: 20),
PlacesControls(),
// Bottom view
BlocBuilder<PlacesBloc, PlacesState>(
// ignore: missing_return
builder: (context, state) {
if (state is Empty) {
return MessageDisplay(
message: 'Search places around you!',
);
} else if (state is Loading) {
return LoadingWidget();
} else if (state is Loaded) {
return PlacesDisplay(places: state.places);
} else if (state is Error) {
return MessageDisplay(
message: state.message,
);
}
},
),
],
),
),
),
);
}
create
参数在我的 Bloc 版本中不存在。
也在我的 Widget
中,我使用 BlocProvider.of<PlacesBloc>(context).add(GetPlacesFromServer());
将事件添加到我的 'BlocProvider',但是这个 .add
方法在我的 Bloc 中不存在。
这是教程视频(底部)和我的(顶部)之间的构造函数差异:
您的教程可能是为库的 4.0.0 版编写的,它应该像您展示的那样工作。
5.0.0和6.0.0版本有很多breaking changes