错误状态:在没有注册事件处理程序的情况下调用了 add Fetch Article

Kis*_*eet 13 flutter

class _HomePageState extends State<HomePage> {
  late ArticleBloc articleBloc;

  @override
  void initState() {
    super.initState();
    articleBloc = BlocProvider.of<ArticleBloc>(context);
    articleBloc.add(FetchArticlesEvent());
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
Run Code Online (Sandbox Code Playgroud)

它以前可以工作,但现在代码不起作用。

我尝试了很多搜索但失败了。

这是错误:

错误状态:在没有注册事件处理程序的情况下调用 add(FetchArticlesEvent)。

dan*_*ngg 23

你的集团版本是什么?也许您正在使用 v8.0.0

说到这里。以及作者的替换建议。

在 bloc v7.2.0 中,mapEventToState 已被弃用,取而代之的是 on。mapEventToState 将在 bloc v8.0.0 中删除。

所以请检查您的集团版本


tol*_*ler 11

您必须按如下方式注册 FetchArticlesEvent 类。如果您尝试使用尚未注册的事件,您将收到此错误。

class ArticleBloc extends Bloc<ArticleEvent, ArticleState> {
  ArticleBloc() : super(ArticleInitial()) {
    on<FetchArticlesEvent>((event, emit) {
      //TODO
    });
  }
}
Run Code Online (Sandbox Code Playgroud)