Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference another file in Dart?

Tags:

dart

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

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?

like image 377
Steoates Avatar asked Oct 18 '12 10:10

Steoates


People also ask

How do I access a variable from another Dart file?

You need to import the file with the variable dateTime. Then you have 2 ways: make the variable static: static var dateTime. for this way you don't need todo know anything.


1 Answers

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'; 

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'; 

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 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 // ... 

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.

like image 102
Matt B Avatar answered Oct 13 '22 06:10

Matt B