扩展Dart中的类时出错:“no_generative_constructors_in_superclass”

jfgube3f  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(29)

我在Dart中遇到了一个与类继承和构造函数相关的问题。我收到的错误消息是:

error: The class 'PathWithActionHistory' can't extend 'Path' because 'Path' only has factory constructors (no generative constructors), and 'PathWithActionHistory' has at least one generative constructor. (no_generative_constructors_in_superclass at [paintroid] lib\core\path_with_action_history.dart:3)

字符串

Code Snippets:

import 'dart:ui';

class PathWithActionHistory extends Path {
  final actions = <PathAction>[];

  @override
  void moveTo(double x, double y) {
    actions.add(MoveToAction(x, y));
    super.moveTo(x, y);
  }

  @override
  void lineTo(double x, double y) {
    actions.add(LineToAction(x, y));
    super.lineTo(x, y);
  }

  @override
  void close() {
    actions.add(const CloseAction());
    super.close();
  }
}


我试图用一个名为PathWithActionHistory的新类扩展Path类。但是,我得到一个错误,指出Path只有工厂构造函数,而PathWithActionHistory至少有一个生成构造函数。我如何解决这个问题?

**附加上下文:**沿着提供的问题,我还得到了其他错误;然而,提到的错误是必须修复的主要错误。

1.错误:缺少'Path. addArc'、'Path. addOval'、'Path. addPath'、'Path. addPolygon'等22个具体实现。
1.错误:方法'moveTo'在超类型中总是抽象的。
1.错误:方法'lineTo'在超类型中始终是抽象的。
1.错误:方法'close'在超类型中总是抽象的。

Flutter Doctor:(更新)

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.13.4, on Microsoft Windows [Version 10.0.22621.2792], locale en-IN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2022.1)
[√] Android Studio (version 2023.1)
[√] VS Code (version 1.85.0)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 1 category

w46czmvw

w46czmvw1#

Path不提供公共的非factory构造函数,所以**它不能用extends**派生。(在Dart 3为类引入interface修饰符之前,给一个类私有构造函数,只有公共的factory构造函数是防止extends子类型化的典型方法。)
你也可以使用implements来创建你自己的Path类,它 Package 一个现有的Path对象并转发给它:

class PathWithActionHistory implements Path {
  final actions = <PathAction>[];

  final Path _path;

  PathWithActionHistory.from(this._path);

  @override
  void moveTo(double x, double y) {
    actions.add(MoveToAction(x, y));
    _path.moveTo(x, y);
  }

  @override
  void lineTo(double x, double y) {
    actions.add(LineToAction(x, y));
    _path.lineTo(x, y);
  }

  @override
  void close() {
    actions.add(const CloseAction());
    _path.close();
  }

  @override
  PathFillType get fillType => _path.fillType;

  @override
  void set fillType(PathFillType value) => _path.fillType = value;

  // ... etc. ...
}

字符串
请注意,使用implements时,您必须提供Path接口的 all 方法(包括属性getter和setter)的实现。虽然工作量更大,但它也更安全,因为如果向Path添加新方法,您的代码将无法编译,而不是在运行时默默地行为不端。

相关问题