[英]undefined_named_parameter in super constructor of abstract class
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.我对 Dart 和 Flutter 非常陌生,我一直在尝试创建两个类(SuccessState 和 ErrorState),它们实现了一个抽象的 class(Data.State),带有可选参数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.出于某种原因,每当我在 SuccessState 和 ErrorState 中调用超级构造函数时,都会在 SuccessState 构造函数中的“data”参数以及 ErrorState 构造函数中的“status”和“msg”参数上收到 undefined_named_parameter 错误。 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);
}
The thing you are doing wrong is your are implementing as you have to extend DataState class then issue will resolved您做错的事情是您正在实施,因为您必须扩展 DataState class 然后问题将得到解决
'class ErrorState implements DataState' '类 ErrorState 实现 DataState'
Change to class ErrorState extends DataState更改为 class ErrorState 扩展 DataState
The problem is you are using implements
rather than extends
.问题是您使用的是implements
而不是extends
。 You don't inherit the super constructor when using implements
.使用implements
时不会继承超级构造函数。
You want to extend SuccessState and ErrorState to DataState.您想将 SuccessState 和 ErrorState 扩展到 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.