I’m encountering an issue in Dart related to class inheritance and constructors. The error message I’m receiving is:
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] libcorepath_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();
}
}
I’m trying to extend the Path
class with a new class called PathWithActionHistory
. However, I’m getting an error indicating that Path
only has factory constructors and PathWithActionHistory
has at least one generative constructor. How can I resolve this issue?
Additional Context:
Along with the provided problem, I am also getting other errors; however, the mentioned error is the primary error that has to be fixed.
- error: Missing concrete implementations of ‘Path.addArc’, ‘Path.addOval’, ‘Path.addPath’, ‘Path.addPolygon’, and 22 more.
- error: The method ‘moveTo’ is always abstract in the supertype.
- error: The method ‘lineTo’ is always abstract in the supertype.
- error: The method ‘close’ is always abstract in the supertype.
Flutter Doctor:(Updated)
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
2
Answers
The error you’re encountering is due to the fact that the Path class only has factory constructors and no generative constructors. To resolve this issue, you need to provide an explicit constructor in your PathWithActionHistory class that calls the appropriate constructor in the superclass (Path).
Here’s an example of how you can modify your PathWithActionHistory class to address the issue:
`
`
This modification introduces a default constructor for PathWithActionHistory that calls the default constructor of the Path class. Additionally, make sure that you have implementations for the abstract methods mentioned in the errors, such as addArc, addOval, addPath, moveTo, lineTo, and close.
Regarding the other errors you’re facing, ensure that you have the necessary Android toolchain components installed and the Android licenses accepted. Follow the suggestions provided by Flutter Doctor to resolve these issues. Additionally, install Visual Studio and the required workloads for developing Windows apps if you intend to use Flutter on Windows.
Path
does not provide a public, non-factory
constructor, so it cannot be derived from withextends
. (Before Dart 3 introduced theinterface
modifier for classes, giving a class private constructors with only publicfactory
constructors was the typical way to prevent subtyping withextends
.)You alternatively can use
implements
to create your ownPath
class that wraps an existingPath
object and forwards to it:Note that by using
implements
, you must provide implementations for all methods (including property getters and setters) of thePath
interface. Although it’s more work, it also safer since if new methods are added toPath
, your code will fail to compile instead of silently misbehaving at runtime.