简体   繁体   English

是否可以在 Dart 中定义一个抽象的命名构造函数?

[英]Is it possible to define an abstract named constructor in Dart?

I'm trying to create an abstract class Firestorable which will ensure that subclasses override a named constructor fromMap(Map<String, dynamic> map)我正在尝试创建一个抽象类Firestorable ,它将确保子类覆盖命名的构造函数fromMap(Map<String, dynamic> map)

The code looks like this ...代码看起来像这样......

abstract class Firestorable {
  /// Concrete implementations will convert their state into a
  /// Firestore safe [Map<String, dynamic>] representation.
  Map<String, dynamic> toMap();

  /// Concrete implementations will initialize its state
  /// from a [Map] provided by Firestore.
  Firestorable.fromMap(Map<String, dynamic> map);
}

class WeaponRange implements Firestorable {
  int effectiveRange;
  int maximumRange;

  WeaponRange({this.effectiveRange, this.maximumRange});

  @override
  WeaponRange.fromMap(Map<String, dynamic> map) {
    effectiveRange = map['effectiveRange'] ?? 5;
    maximumRange = map['maximumRange'] ?? effectiveRange;
  }

  @override
  Map<String, int> toMap() {
    return {
      'effectiveRange': effectiveRange,
      'maximumRange': maximumRange ?? effectiveRange,
    };
  }
}

I don't get any errors when I do this, however I also don't get a compile error when I leave out the concrete implementation of the fromMap(..) constructor.当我这样做时,我没有收到任何错误,但是当我忽略fromMap(..)构造函数的具体实现时,我也没有收到编译错误。

For example the following code will compile without any errors:例如,以下代码将编译而不会出现任何错误:

abstract class Firestorable {
  /// Concrete implementations will conver thier state into a
  /// Firestore safe [Map<String, dynamic>] representation.
  Map<String, dynamic> convertToMap();

  /// Concrete implementations will initialize its state
  /// from a [Map] provided by Firestore.
  Firestorable.fromMap(Map<String, dynamic> map);
}

class WeaponRange implements Firestorable {
  int effectiveRange;
  int maximumRange;

  WeaponRange({this.effectiveRange, this.maximumRange});

//   @override
//   WeaponRange.fromMap(Map<String, dynamic> map) {
//     effectiveRange = map['effectiveRange'] ?? 5;
//     maximumRange = map['maximumRange'] ?? effectiveRange;
//   }

  @override
  Map<String, int> convertToMap() {
    return {
      'effectiveRange': effectiveRange,
      'maximumRange': maximumRange ?? effectiveRange,
    };
  }
}

Am I not able to define an abstract named constructor and have it be a require implementation in a concrete class?我不能定义一个抽象的命名构造函数并且让它成为一个具体类中的 require 实现吗? If not, what would be the correct way to do this?如果没有,这样做的正确方法是什么?

As stated in the official guide for the Dart Language constructors aren't inherited so you can't enforce a factory constructor to subclasses.正如 Dart 语言构造函数的官方指南中所述,它不是继承的,因此您不能对子类强制使用工厂构造函数。 To ensure an implementation it should be part of the class' interface which constructors are not.为了确保实现,它应该是类的接口的一部分,而构造函数不是。 You can check these related stackoverflow questions for more information:您可以查看这些相关的 stackoverflow 问题以获取更多信息:

How to declare factory constructor in abstract classes 如何在抽象类中声明工厂构造函数

How do i guarentee a certain named constructor in dart 我如何保证 dart 中的某个命名构造函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dart 构造函数给我 null 个值 - Dart Constructor gives me null values 默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor 是否可以在Firebase数据库引用路径上定义变量? - Is it possible to define a variable on the firebase database references path? Android Firebase数据库异常:未定义无参数构造函数 - Android Firebase Database exception: not define a no-argument constructor Class android.location.Location 没有定义无参构造函数 - Class android.location.Location does not define a no-argument constructor 是否可以为子对象定义limit()? - Is it possible to define limit()'s for child objects? 是否可以创建一个具有空构造函数的 Parcelable Class? - Is it possible to create a Parcelable Class that has an empty constructor? 是否可以在后端编写 C# 代码并将结果发送回 dart? - It it possible to write C# code in backend and send results back to dart? 我正在尝试将我的 flutter 应用程序与 firebase 连接,但它不会生成名为“Generated_plugins_registrant.dart”的文件 - I am trying to connect my flutter app with firebase but it doesn't generate the file named "Generated_plugins_registrant.dart" 将数据从 Firebase 提取到 RecyclerView 时出错。 致命异常:...没有定义无参数构造函数 - Error in fetching data from Firebase to RecyclerView. FATAL EXCEPTION: ... does not define a no-argument constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM