Duplicate Class in Dart

2019-07-19 06:57发布

问题:

I am writing a simple application to learn some basic Dart programming but I can't work out the structure and inclusions - I get a Duplicate class Point

First of all, I have my main class I called MouseTrack. It initializes the list and will have a loop.

#import('dart:html');
#import('Eye.dart');

class MouseTrace {
List<Eye> eyes;
...
}

Secondly, I have a class called Eye, which is supposed to hold information of of an eye as a circle. It's quite simple:

#library('app:eye');

#import('dart:html'); // without this one, I get no error but I want to have it to use CanvasRenderingContext2D

#import('Point.dart');

class Eye {
  Point position;
  num radius;

  Eye() :
      position = new Point() {
  }

  void draw(CanvasRenderingContext2D context) {
    // draws a circle
  }
}

And finally Point:

#library('app:point');

class Point {
    num x, y;

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

What I want to achieve is 3 separate classes - main, Eye and Point, so I can have instances of Eye in main (for simplicity & nice model) and instances of Point in Eye (for storing position). At least that's how I'm used to doing.

P.S I know I can skip types but I want it there and I guess it's a problem with inclusions rather than the language (and want to fix it so I know how to do it properly). P.S.S. I have cut off some code so that you don't have to read everything but, if you want, I will post it all.

回答1:

Problem lies in the app:eye library (in the Eye.dart file). You import dart:html and app:point libraries, but both of them define a Point class. This situation is invalid. You can solve it either by not importing dart:html at all, if you don't need it, or prefixing one of those imports:

#import('dart:html', prefix: 'html');
#import('Point.dart');

In this case, you will have to refer to names from dart:html by using a html. prefix. In your case, if you want to use the CanvasRenderingContext2D class, you will have to write html.CanvasRenderingContext2D instead.