Error: The superclass, 'Bloc', has no unnamed constructor that takes no arguments. class OverViewListBloc extends Bloc<OListEvent, List<OList>> {

Error: The superclass, 'Bloc', has no unnamed constructor that takes no arguments. class OverViewListBloc extends Bloc<OListEvent, List<OList>> {

为什么会出现这个错误?:

Error: The superclass, 'Bloc', has no unnamed constructor that takes no arguments.
class OverViewListBloc extends Bloc<OListEvent, List<OList>> {

我可以用 OverViewListBloc(List<OList> initialState) : super(initialState); 修复它,但我不想这样修复它,因为我必须给出一个论点。

这是我的代码:

class OverViewListBloc extends Bloc<OListEvent, List<OList>> {
  OverViewListBloc(List<OList> initialState) : super(initialState);

  List<OList> get initialState => List<OList>();

  @override
  Stream<List<OList>> mapEventToState(OListEvent event) async* {
    if (event is SetList) {
      yield event.foodList;
    } else if (event is AddList) {
      List<OList> newState = List.from(state);
      if (event.newolist != null) {
        newState.add(event.newolist);
      }
      yield newState;
    } else if (event is DeleteList) {
      List<OList> newState = List.from(state);
      newState.removeAt(event.olistindex);
      yield newState;
    } else if (event is UpdateList) {
      List<OList> newState = List.from(state);
      newState[event.listIndex] = event.newolist;
      yield newState;
    }
  }
}

还有其他修复方法吗?

Bloc 更改了设置初始状态的方式:

不再需要此行:

List<OList> get initialState => List<OList>();

而是将该值传递给构造函数:

OverViewListBloc() : super(List<OList>());

随着新的 功能弃用 List<> 的空构造函数,您可以只使用

OverViewListBloc() : super(<OList>[]);