Home > Enterprise >  undefined_named_parameter in super constructor of abstract class
undefined_named_parameter in super constructor of abstract class

Time:12-17

I am very new to Dart and Flutter, and I've been trying to make two classes (SuccessState and ErrorState) that implement an abstract class (DataState) with optional named parameters. For some reason, whenever I call the super constructor in SuccessState and ErrorState, I get an undefined_named_parameter error on the "data" parameter in the SuccessState constructor and "status" and "msg" parameters in the ErrorState constructor. Any input is greatly appreciated, thanks.

abstract class DataState<T> {
  final T? data;
  final int? status;
  final String? msg;

  const DataState({this.data, this.status, this.msg});
}

class SuccessState<T> implements DataState<T> {
  const SuccessState(T data) : super(data: data);

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class ErrorState<T> implements DataState<T> {
  const ErrorState(int status, String msg) : super(status: status, msg: msg);

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

CodePudding user response:

The problem is you are using implements rather than extends. You don't inherit the super constructor when using implements.

CodePudding user response:

The thing you are doing wrong is your are implementing as you have to extend DataState class then issue will resolved

'class ErrorState implements DataState'

Change to class ErrorState extends DataState

CodePudding user response:

You want to extend SuccessState and ErrorState to DataState.

abstract class DataState<T> {
  final T? data;
  final int? status;
  final String? msg;

  const DataState({this.data, this.status, this.msg});
}

class SuccessState<T> extends DataState<T> {
  SuccessState(T data) : super(data: data);

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class ErrorState<T> implements DataState<T> {
  const ErrorState(int status, String msg) : super();

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
  • Related