Dart library layout

2019-07-04 02:03发布

问题:

I'm struggling with Dart library layout. I tried the following

lib/
  A.dart
  B.dart
  my_lib.dart

where: A.dart

class A {
  B myB;   
}

B.dart

class A {
  B myB;   
}

my_lib.dart

#library('my_lib');
#source('A.dart');
#source('B.dart');  

But in A.dart, in Dart Editor there is a problem: B - no such type. If I import B.dart in that file, via

#import('B.dart)',

but now it claims that part of library can only contain part directive. According to http://news.dartlang.org/2012/07/draft-spec-changes-to-library-and.html

partDirective:
  metadata part  stringLiteral “;”
;

But that doesn't work for me either. What am I missing?

回答1:

Download the latest SDK and try:

a.dart

class A {
  B myB;
}

b.dart

class B {

}

lib.dart

library mylib;

part 'a.dart';
part 'b.dart';

That should work.



回答2:

Due to new release changes the layout has to look like the followed example:

a.dart

part of mylib;

class A {
  B myB;
}

b.dart

part of mylib;

class B {

}

lib.dart

library mylib;

part 'a.dart';
part 'b.dart';


标签: dart