skip to Main Content

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.

  1. error: Missing concrete implementations of ‘Path.addArc’, ‘Path.addOval’, ‘Path.addPath’, ‘Path.addPolygon’, and 22 more.
  2. error: The method ‘moveTo’ is always abstract in the supertype.
  3. error: The method ‘lineTo’ is always abstract in the supertype.
  4. 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


  1. 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:

    `

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

    `
    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.

    Login or Signup to reply.
  2. Path does not provide a public, non-factory constructor, so it cannot be derived from with extends. (Before Dart 3 introduced the interface modifier for classes, giving a class private constructors with only public factory constructors was the typical way to prevent subtyping with extends.)

    You alternatively can use implements to create your own Path class that wraps an existing Path object and forwards to it:

    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. ...
    }
    

    Note that by using implements, you must provide implementations for all methods (including property getters and setters) of the Path interface. Although it’s more work, it also safer since if new methods are added to Path, your code will fail to compile instead of silently misbehaving at runtime.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search