【发布时间】:2022-01-07 13:59:26
【问题描述】:
我正在将我的 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 {}
拜托,非常感谢我能得到的所有帮助。
【问题讨论】:
标签: flutter dart bloc state-management flutter-bloc