[英]Error: Type argument 'RoutesBloc' doesn't conform to the bound 'BlocBase<S>' of the type variable 'B' on 'BlocBuilder'
[英]Migrating my bloc app to null safety. error: 'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B'
我正在将我的 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();
...
state。
@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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.