且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dart-需要说明库/零件以及导入/导出

更新时间:2023-11-27 08:48:58

默认为定义一个键入每个文件,而不使用 part ,而仅导入所需的文件。这涵盖了大多数用例。

Default to defining one type per file, not using part, and importing only the files you need. This covers the majority of use cases.

现在,假设您有两种常用的类型,例如 Thing 和一个 ThingException Thing 做不好的事情时抛出。将这两个文件都导入到各处都很繁琐,因此您需要权衡以下三个选择:

Now, let's say you have two types that are commonly used together - for example, a Thing and a ThingException that gets thrown when Thing does bad things. Importing both of these files everywhere is tedious, so you have three options with their own tradeoffs:


  1. 在同一声明中同时声明这两种类型文件

  2. 在自己的文件中声明每种类型,并让主文件导出另一种。因此, thing.dart 导出 thing_exception.dart 。导入 thing.dart 可以使导入文件同时访问这两个文件。

  3. 在自己的文件中声明每种类型,并将另一个文件作为主文件的一部分。因此, thing_exception .dart 声明它是 thing.dart 的一部分。导入 thing.dart 可以使导入文件访问两个文件。

  1. Declare both types in the same file.
  2. Declare each type in its own file, and have the 'primary' file export the other. So, thing.dart exports thing_exception.dart. Importing thing.dart gives the importing file access to both.
  3. Declare each type in its own file, and have the other file be a 'part of' the primary file. So, thing_exception.dart declares that it is 'part of' thing.dart. Importing thing.dart gives the importing file access to both files.

对于这种简单类型及其例外,***的选择是使用选项1。当代码量增加或两种类型都增加时可见度有所不同,因此此选项的吸引力降低。这将选项2和3放在桌子上。

For this simple type and its exception, your best bet is to use option 1. When the amount of code grows or the two types diverge in visibility, this option becomes less attractive. This puts options 2 and 3 are on the table.

当您拥有单独的文件时,选项2通常比选项3更好,因为它可以保持一定的灵活性-您可以导入 thing_exception.dart 而不是 thing.dart 。如果使用选项3,则无法执行此操作-您要么导入所有零件,要么不导入任何零件。这是您在尝试制作零件并导入同一文件时看到的错误。

When you have separate files, option 2 is often a better approach than options 3 because you maintain some flexibility - you could only import thing_exception.dart and not thing.dart. If you use option 3, you can't do this - you either import all of the parts or none of them. This is the error you are seeing when trying to do a part and import in the same file.

当两个文件中的代码高度匹配时,选项3变得很有价值彼此依赖,他们需要能够访问彼此的私有成员。

Option 3 becomes valuable when you the code is in the two files is highly dependent on one another and they need the ability to access private members of each other. This is less common.

当您拥有一堆这样的文件时,从更传统的意义上来说,它成为一个库。您声明一个主库文件(您的我的lib.dart 文件),该文件导出文件:

When you have a bunch of files like this together, it becomes a 'library' in the more traditional sense. You declare a main library file (your my lib.dart file) that exports files:

export 'public.dart';
export 'other_public.dart';

bin 脚本将库导入为整体,但看不到未从 my_lib.dart 显式导出的任何内容。

The bin script imports the library as a whole, but it can't see anything that isn't explicitly exported from my_lib.dart.

import 'package:mylib/mylib.dart';

这是一个小包装的示例,该示例结合使用了这三个选项作为参考。

Here's an example of a smallish package that uses all three of these options together for a good reference.