Migrating my bloc app to null safety. error: 'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B'
Migrating my bloc app to null safety. error: 'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B'
我正在将我的 flutter 应用程序迁移到 null-safety,但我在 BlocProvider 和 BlocBuilder 上遇到了这个错误。
'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B'.
Try using a type that is or is a subclass of 'BlocBase<AssignmentState>'.
我已经检查了类似问题的解决方案,在我看来我已经按照他们的建议做了。不过我可能遗漏了一些东西,我需要帮助。
小部件。
class _AssignmentScreenWidgetState extends State<_AssignmentScreenWidget> {
AssignmentBloc? _bloc;
bool assignmentAdd = false;
@override
void initState() {
super.initState();
_bloc = BlocProvider.of<AssignmentBloc>(context)
..add(FetchEvent(widget.courseId, widget.assignmentId));
}
final GlobalKey<AssignmentDraftWidgetState> assignmentDraftWidgetState =
GlobalKey<AssignmentDraftWidgetState>();
@override
Widget build(BuildContext context) {
return BlocListener<AssignmentBloc, AssignmentState>( // where the errors are
bloc: _bloc,
listener: (BuildContext context, AssignmentState state) {
if (state is CacheWarningAssignmentState) {
showDialog(
context: context, builder: (context) => WarningLessonDialog());
}
},
child: BlocBuilder<AssignmentBloc, AssignmentState>( // where the errors are
builder: (context, state) {
return Scaffold(
...
集团。
class AssignmentBloc extends Bloc<AssignmentEvent, AssignmentState?> {
final AssignmentRepository _assignmentRepository;
final CacheManager cacheManager;
AssignmentBloc(this._assignmentRepository, this.cacheManager) : super(null);
@override
AssignmentState get initialState => InitialAssignmentState();
...
州。
@immutable
abstract class AssignmentState {}
class InitialAssignmentState extends AssignmentState {}
class LoadedAssignmentState extends AssignmentState {
final AssignmentResponse assignmentResponse;
LoadedAssignmentState(this.assignmentResponse);
}
class ErrorAssignmentState extends AssignmentState {}
class CacheWarningAssignmentState extends AssignmentState {}
非常感谢我能得到的所有帮助。
您的 _bloc 变量实际上不可为空。它只是在构造函数中不可用。
所以让它成为 late
而不是可空的:
late AssignmentBloc _bloc;
这应该可以解决你以后的问题,因为现在你的 bloc 的类型参数不再是 AssignmentBloc?
而是一个真正的 AssignmentBloc
.
我正在将我的 flutter 应用程序迁移到 null-safety,但我在 BlocProvider 和 BlocBuilder 上遇到了这个错误。
'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B'.
Try using a type that is or is a subclass of 'BlocBase<AssignmentState>'.
我已经检查了类似问题的解决方案,在我看来我已经按照他们的建议做了。不过我可能遗漏了一些东西,我需要帮助。
小部件。
class _AssignmentScreenWidgetState extends State<_AssignmentScreenWidget> {
AssignmentBloc? _bloc;
bool assignmentAdd = false;
@override
void initState() {
super.initState();
_bloc = BlocProvider.of<AssignmentBloc>(context)
..add(FetchEvent(widget.courseId, widget.assignmentId));
}
final GlobalKey<AssignmentDraftWidgetState> assignmentDraftWidgetState =
GlobalKey<AssignmentDraftWidgetState>();
@override
Widget build(BuildContext context) {
return BlocListener<AssignmentBloc, AssignmentState>( // where the errors are
bloc: _bloc,
listener: (BuildContext context, AssignmentState state) {
if (state is CacheWarningAssignmentState) {
showDialog(
context: context, builder: (context) => WarningLessonDialog());
}
},
child: BlocBuilder<AssignmentBloc, AssignmentState>( // where the errors are
builder: (context, state) {
return Scaffold(
...
集团。
class AssignmentBloc extends Bloc<AssignmentEvent, AssignmentState?> {
final AssignmentRepository _assignmentRepository;
final CacheManager cacheManager;
AssignmentBloc(this._assignmentRepository, this.cacheManager) : super(null);
@override
AssignmentState get initialState => InitialAssignmentState();
...
州。
@immutable
abstract class AssignmentState {}
class InitialAssignmentState extends AssignmentState {}
class LoadedAssignmentState extends AssignmentState {
final AssignmentResponse assignmentResponse;
LoadedAssignmentState(this.assignmentResponse);
}
class ErrorAssignmentState extends AssignmentState {}
class CacheWarningAssignmentState extends AssignmentState {}
非常感谢我能得到的所有帮助。
您的 _bloc 变量实际上不可为空。它只是在构造函数中不可用。
所以让它成为 late
而不是可空的:
late AssignmentBloc _bloc;
这应该可以解决你以后的问题,因为现在你的 bloc 的类型参数不再是 AssignmentBloc?
而是一个真正的 AssignmentBloc
.