1

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

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;
    }
  }
}

还有其他修复吗?

4

1 回答 1

1

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

不再需要此行:

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

相反,您将该值传递给构造函数:

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

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

OverViewListBloc() : super(<OList>[]);
于 2021-01-02T15:14:44.453 回答