Dart-需要说明库/零件以及导入/导出 [英] Dart - Need explanation of library/part and import/export
问题描述
是的,我阅读了> dart导入和同一文件
我具有以下结构:
- lib /
- src /
- 一个/
- SomeClass.dart
- one.dart
我正在尝试实现以下行为:
I'm trying to achieve this behavior:
- 所有公共变量和隐藏变量都已完全
- main.dart可以访问库中的所有公共变量。
有一个问题。由于某些奇怪的原因,我不能在 part of中使用任何指令。所以我不能在
one.dart
中使用它:There is a problem. For some weird reason I can't use any directive with 'part of'. So I can't use this in the
one.dart
:part of mylib; import 'SomeClass.dart'; //somecode
所以我要么需要从
SomeClass.dart
到one.dart
(这会使代码的可读性和混淆性降低),或者我需要将mylib.dart
So I either need to move class definition from
SomeClass.dart
toone.dart
(and that will make code less readable and mixed up) or I need to move 'import' in themylib.dart
.library mylib; import 'SomeClass.dart'; part ..
我不喜欢这两个选项。在第二种情况下,我将需要解析所有模块,然后移动导入/导出。肯定会破坏某些东西。
I don't like either of the options. In the second case I will need to parse all modules then and move import/exports. Which will definitely break something.
这听起来很奇怪,但是该项目将自动从各种模块中构建。而
one /
就是其中之一。It may sound weird but the project will build from various modules automatically. And
one/
is one of them.这个应用程序设计很糟糕,我知道。但是我要么需要找到一种更好的方法,要么只是公开所有变量,而不必打扰。
This app design is bad, I know. But either I need to find a better way or just make all variables public and don't bother.
推荐答案
默认为定义一个键入每个文件,而不使用
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 aThingException
that gets thrown whenThing
does bad things. Importing both of these files everywhere is tedious, so you have three options with their own tradeoffs:- 在同一声明中同时声明这两种类型文件。
- 在自己的文件中声明每种类型,并让主文件导出另一种。因此,
thing.dart
导出thing_exception.dart
。导入thing.dart
可以使导入文件同时访问这两个文件。 - 在自己的文件中声明每种类型,并将另一个文件作为主文件的一部分。因此,
thing_exception .dart
声明它是thing.dart
的一部分。导入thing.dart
可以使导入文件访问两个文件。
- Declare both types in the same file.
- Declare each type in its own file, and have the 'primary' file export the other. So,
thing.dart
exportsthing_exception.dart
. Importingthing.dart
gives the importing file access to both. - 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
. Importingthing.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 notthing.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 frommy_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.
这篇关于Dart-需要说明库/零件以及导入/导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 一个/
- src /