如何在 Dart 中引用另一个文件? [英] How to reference another file in Dart?

查看:33
本文介绍了如何在 Dart 中引用另一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用 libraryimport 甚至 #import,但哪个是正确的?

I know you can use the library, import and even #import, but which is correct?

我有两个文件,MainClass.dartLibrary.Dart,我想在其中添加对 Library.dart 的引用MainClass.dart.我该怎么做?

I have got two files, MainClass.dart and Library.Dart, and I want to add a reference to Library.dart in MainClass.dart. How can I do that?

推荐答案

首先让我先说请不要在导入或库或其他任何东西之前使用哈希符号.这是一种正在被弃用的旧语法.所以我们不再想使用 #import('...') 正确的语法是:

Firstly let me just preface this by saying please do not use the hash symbol before import or library or anything else. This is an old syntax that is being deprecated. So we no longer want to use #import('...') The correct syntax is:

import 'some_file.dart';

也就是说,我们可以通过两种不同的方式访问当前文件中的不同 dart 源文件.首先是import文件.当您想将不同的库带入当前文件(或更准确地说是当前库)时,我们会使用它.

That said, there are two different things we can do to access different dart source files within our current file. The first is to import the file. We use this such as in your case when you want to bring a different library into the current file (or more accurately current library).

通常,如果您的文件位于同一目录或当前目录的子目录中,我们会像这样导入它们:

Usually if your files are in the same directory, or a sub directory of the current one we would import them like this:

import 'lib/library.dart';

但是,如果您使用 pub 包布局,您还可以使用一些特殊的快捷方式引用来导入文件(特别是从您导入的其他包中).我强烈建议阅读发布网站上的文档,因为大多数应用程序和库的设计考虑到了这一点.它还提供了最佳命名约定的建议,例如文件名全部小写,使用下划线表示空格和目录布局.

However If you are using the pub package layout you can also use some special short-cut references as well to import files (particularly from other packages you've imported). I highly suggest reading the documents on the pub site, as most applications and libraries are designed with this in mind. It also has suggestions on best naming conventions such as filenames in all lower case, and using underscore for spaces, and directory layouts.

关于将 dart 文件带入另一个文件的另一个重要事项是,我们可以使用 partpart of 指令.这曾经被称为 #source 但已更改(删除哈希符号)以减少混淆.part 指令用于编写跨越多个文件的单个库.例如,假设您有一个 Awesome Library,对于单个文件来说它开始变得有点大.我们将创建库的主文件(不要与 main 方法混淆).此文件通常与库本身具有相同的名称.

The other important thing to know about bringing a dart file into another file, is that we can use the part and part of directives. This used to be called #source but was changed (with the removal of the hash sign) to reduce confusion. The part directive is used when we want to write a single library which spans multiple files. Say for instance you have an Awesome Library, which is starting to get a little large for a single file. We will create the main file of the library (not to be confused with the main method). This file will usually have the same name as the library itself.

// awesome_library.dart
library awesome_library;

import 'dart:math';
import '...';

// this injects all the content of secret_file.dart
// into this file right here almost as if it was
// here in the first place.
part 'src/secret_file.dart';

// The rest of our file here
// ...

part 指令基本上从我们的 src/secret_file.dart 中获取所有内容并将其插入到文件的该部分中.这使我们能够将庞大的Awesome Library 拆分为多个更易于维护的较小文件.虽然没有特别要求,但在我们的 secret_file.dart 中使用 part of 指令有助于编辑器知道它是库的一部分".

The part directive basically takes everything from our src/secret_file.dart and inserts it into that part of the file. This allows us to split our huge Awesome Library into multiple smaller files that are easier to maintain. While not specifically required, it is helpful to use the part of directive in our secret_file.dart to help the editor know that it is "part of" the library.

// secret_file.dart
part of awesome_library;

// ... Rest of our secret_file code below.

请注意,当使用这样的零件文件时,零件(即不是库主文件的所有内容)不能自己导入或使用库声明.他们导入任何导入到主文件中的内容,但不能添加任何额外的导入.

Note that when using a part file like this, the part(s) (that is everything that is not the main file of the library) cannot import or use library declarations themselves. They import whatever is imported into the the main file, but they cannot add any additional imports.

有关图书馆的更多信息,请参阅此链接.

这篇关于如何在 Dart 中引用另一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆