Search code examples
flutterrun-app

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


Why did this error occur?:

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

I could fix it with OverViewListBloc(List<OList> initialState) : super(initialState);, but I don't want to fix it this way, because I have to give an argument.

This is my code:

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

Is there any other fix?


Solution

  • Bloc changed the way it sets the initial state:

    This line is no longer needed:

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

    instead you pass that value into the constructor:

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

    With the new feature deprecating the empty constructor of List<>, you can just use

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