我在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
型
1条答案
按热度按时间w46czmvw1#
Path
不提供公共的非factory
构造函数,所以**它不能用extends
**派生。(在Dart 3为类引入interface
修饰符之前,给一个类私有构造函数,只有公共的factory
构造函数是防止extends
子类型化的典型方法。)你也可以使用
implements
来创建你自己的Path
类,它 Package 一个现有的Path
对象并转发给它:字符串
请注意,使用
implements
时,您必须提供Path
接口的 all 方法(包括属性getter和setter)的实现。虽然工作量更大,但它也更安全,因为如果向Path
添加新方法,您的代码将无法编译,而不是在运行时默默地行为不端。