我正在将我的flutter应用程序迁移到零安全,我在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变量实际上不可为null。它只是在构造函数中不可用。
因此,将其设为late
,而不是可为null:
late AssignmentBloc _bloc;
这应该可以解决您以后的问题,因为现在您的块的类型参数不再是AssignmentBloc?
,而是真正的AssignmentBloc
。