未调用 Dart 方法

Dart method not called

我在使用 BLoC

的 dart/flutter 项目中有以下代码
abstract class BasePage extends StatelessWidget {
  Widget get body;

  const BasePage({@required Key key}) : super(key: key);

  Bloc create(BuildContext context) {
    final dao = Provider.of<LessonsDao>(context, listen: false);
    return LessonListBloc(dao)..add(LoadListEvent());
  }

  @override
  Widget build(BuildContext context) {
    final blocs = createBlocs(context);
    return BlocProvider(
      lazy: false,
      create: (context) {
        return create(context);
      },
      child: Material(child: body),
    );
  }

}

现在,当我想将创建的函数 return 类型更改为 Bloc 时,订阅者小部件会监听 LessonListBloc 中 yield 的 LessonsListLoaded,未传送到 body 小部件:

Bloc create(BuildContext context) {
    final dao = Provider.of<LessonsDao>(context, listen: false);
    return LessonListBloc(dao)..add(LoadListEvent());
}

您依赖 LessonListBloc

但是BlocBuilder获取不到

情况是您在创建函数 LessonListBloc -> Bloc 中缩小了类型

这会起作用

create(...)
LessonListBloc create(...)

这不会

Bloc create(...)

用类型重写你的代码,分析器会弹出类型不匹配的错误

BlocProvider<LessonListBloc>(...