如何从导入的库中创建一个函数/类,可以访问下面在Dart中导入的所有库?

时间:2014-01-20 09:42:32

标签: dart

假设我有这个:

benchmark.dart:

library benchmark;
benchmark() {...}

app.dart:

import 'benchmark.dart'; // functions from this lib are now accessible in this file
export 'benchmark.dart'; // does this make them accessible in all files imported below?

import 'model.dart';

void main() {
  doSomething();
}

model.dart

doSomething() {
  benchmark(); // => Error, no such method, unless
               //    I import 'benchmark.dart' above in this file!
}

这是正确的行为吗?如何在没有导入 model.dart 中的库的情况下使基准可访问?

1 个答案:

答案 0 :(得分:2)

您必须在每个要使用其他库的库中进行导入 您可能要做的是使用part 'model.dart';(父文件)和part of app;(链接文件)将多个文件连接到一个库。
然后,在该库的所有文件中都可以使用父文件中导入的类型和函数。