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

问题描述 投票:0回答:1

我在 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)

代码片段:

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();
  }
}

我正在尝试用一个名为

Path
的新类来扩展
PathWithActionHistory
类。但是,我收到一条错误,表明
Path
仅具有工厂构造函数,而
PathWithActionHistory
至少具有一个生成构造函数。我该如何解决这个问题?

其他背景: 除了提供的问题之外,我还收到其他错误;然而,上述错误是必须修复的主要错误。

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

颤医生:

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 31.0.0)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] 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`enter code here` 1.85.0)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 2 categories.
flutter dart inheritance constructor path
1个回答
0
投票

您遇到的错误是由于 Path 类只有工厂构造函数而没有生成构造函数。要解决此问题,您需要在 PathWithActionHistory 类中提供显式构造函数,该构造函数调用超类 (Path) 中的相应构造函数。

以下示例说明了如何修改 PathWithActionHistory 类来解决该问题:

`

dart
Copy code
import 'dart:ui';

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

  PathWithActionHistory() : super(); // Call the default constructor of Path

  @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();
  }
}

// Define your PathAction classes here if they are not already defined
class PathAction {}

class MoveToAction extends PathAction {
  final double x;
  final double y;

  MoveToAction(this.x, this.y);
}

class LineToAction extends PathAction {
  final double x;
  final double y;

  LineToAction(this.x, this.y);
}

class CloseAction extends PathAction {
  const CloseAction();
}

` 此修改为 PathWithActionHistory 引入了一个默认构造函数,该构造函数调用 Path 类的默认构造函数。此外,请确保您具有错误中提到的抽象方法的实现,例如 addArc、addOval、addPath、moveTo、lineTo 和 close。

关于您遇到的其他错误,请确保您已安装必要的 Android 工具链组件并接受 Android 许可证。按照 Flutter Doctor 提供的建议来解决这些问题。此外,如果您打算在 Windows 上使用 Flutter,请安装 Visual Studio 和开发 Windows 应用程序所需的工作负载。

© www.soinside.com 2019 - 2024. All rights reserved.