简体   繁体   English

抽象 class 的超级构造函数中的 undefined_named_parameter

[英]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.

相关问题 怎么修? 错误:未定义命名参数“child”。 (undefined_named_parameter 在 [application_1] - How to fix? error: The named parameter 'child' isn't defined. (undefined_named_parameter at [application_1] 错误:未定义命名参数“title”。 ([ecommerce_flutter] lib\screens\main.dart:127 处的 undefined_named_pa​​rameter) - error: The named parameter 'title' isn't defined. (undefined_named_parameter at [ecommerce_flutter] lib\screens\main.dart:127) 超级构造函数语法问题 - 错误:没有具有名称的命名参数 - Super constructor syntax issues - Error: No named parameter with the name Dart - 使用超级调用命名构造函数与 class 名称 - Dart - Call Named Constructor using super vs class name Dart 在工厂命名构造函数中初始化超级构造函数 - Dart Initialise Super Constructor in Factory Named Constructor 是否可以在 Dart 中定义一个抽象的命名构造函数? - Is it possible to define an abstract named constructor in Dart? 命名的可选和默认构造函数参数 - Named optional and default constructor parameter 将“键”转换为超级参数警告——如何更改现有的超级调用以使用超级命名参数? - Convert 'key' to a super parameter warning -- how to change existing super calls to use super named parameters? 将抽象类的扩展类作为参数传递给 flutter - Pass extended class of an abstract class as a parameter in flutter 无效的构造函数名称和未定义的类 - Invalid constructor name and Undefined class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM