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

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

问题描述

这听起来像一个愚蠢的问题,但我真的没有线索,我知道你可以使用 import #import 但是这是正确的?



所以我有两个文件 - MainClass。 dart和Library.Dart - 我想在 MainClass.dart 中添加 Library.dart p>

任何帮助都很好

解决方案

说请不要在导入或库或其他任何之前使用哈希符号。这是一个正在贬值的旧语法。所以我们不再想使用 #import('...')正确的语法是:

  import'some_file.dart'; 

也就是说,我们可以在当前文件中访问不同的dart源文件。第一个是 import 该文件。我们使用这个,如在你的情况下,当你想把一个不同的库到当前文件(或更精确的当前库)。



通常,如果您的文件在同一目录或当前目录的子目录中,我们将这样导入:

  import'lib / library.dart'; 

但是如果你使用pub包布局,你也可以使用一些特殊的快捷引用以导入文件(特别是从您导入的其他软件包)。我强烈建议您阅读酒吧网站上的文档,因为大多数应用程序和库都是基于此设计的。它还有关于最佳命名约定的建议,例如所有小写的文件名,以及使用下划线作为空格和目录布局。



另一个重要的事情, dart文件到另一个文件中,我们可以使用部分部分指令。这以前被称为 #source ,但是更改(删除哈希符号),以帮助一些混乱。当我们要编写跨越多个文件的单个库时,使用 part 指令。例如,您有一个 Awesome库,它开始对单个文件有点大。我们将创建库的主文件(不要与主方法混淆)。此文件通常与库本身具有相同的名称。

  // awesome_library.dart 
library awesome_library;

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

//这里注入了secret_file.dart
//的所有内容到这个文件里就好像是
//这里的第一位。
part'src / secret_file.dart';

//文件的其余部分
// ...


$ b b

part指令基本上从我们的src / secret_file.dart中取得,并把它插入到文件的那个部分。这样,我们就可以将庞大的真棒库拆分为更容易维护的多个较小的文件。虽然没有特别要求,但在我们的secret_file.dart中使用指令的一部分来帮助编辑知道它是库的一部分是有帮助的。

  // secret_file.dart 
awesome_library的一部分;

// ...下面我们的secret_file代码。

请注意,当使用这样的零件文件时,零件不是库的主文件)不能导入或使用库声明本身。他们导入任何导入到主文件,但他们不能添加任何额外的导入。



有关图书馆的详情,请参阅此链接。


This might sound like a stupid question but I honestly have no clue, I know you can use the library, import and even #import but which is correct?

So I've got two files--MainClass.dart and Library.Dart--I want to add a reference to Library.dart in MainClass.dart?

Any help would be great

解决方案

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 depreciated. So we no longer want to use #import('...') The correct syntax is:

import 'some_file.dart';

That said, there are two different things we can do with accessing 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';

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.

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 help with some 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
// ...

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.

For more information about library see this link.

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

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