what's the different between full path and relative path in dart - dart

I develop a flutter app, define serveral models in 'model' package.
Then I declare a class Example in 'model' for example.
model/example.dart
class Example {
#override
String toString() {
return 'class example';
}
}
test_a.dart
import 'package:example/model/example.dart'
Example testA() {
return Example()
}
test.dart
import 'model/example.dart'
import 'test_a.dart'
test() {
Example example = testA();
if (example is Example) {
print('this class is Example');
} else {
print('$example');
}
}
I will get output class example🌚
If I change from import 'model/example.dart' to import 'package:example/model/example.dart' in test.dart, then I will get the output this class is Example.
So I'm confused what is different between the full path and relative path in dart.

package imports
'package:... imports work from everywhere to import files from lib/*.
relative imports
Relative imports are always relative to the importing file.
If lib/model/test.dart imports 'example.dart', it imports lib/model/example.dart.
If you want to import test/model_tests/fixture.dart from any file within test/*, you can only use relative imports because package imports always assume lib/.
This also applies for all other non-lib/ top-level directories like drive_test/, example/, tool/, ...
lib/main.dart
There is currently a known issue with entry-point files in lib/* like lib/main.dart in Flutter. https://github.com/dart-lang/sdk/issues/33076
Dart always assumed entry-point files to be in other top-level directories then lib/ (like bin/, web/, tool/, example/, ...).
Flutter broke this assumption.
Therefore you currently must not use relative imports in entry-point files inside lib/
See also
How to reference another file in Dart?

Related

ModuleNameMismatch mystery

Using rascal from the command line, when I type:
import demo::hello
I receive a ModuleNameMismatch error, although the current directory (the one in which I type java -jar rascal-shell-stable.jar) contains a subdirectory named demo containing a file hello.rsc :
module hello
import IO;
void hello(){
println("Hello word");
}
It however works when I type
import hello
and the hello.rsc is in the current directory.
For your info: I am on Windows 10.
The module name must be the full relative path to the module.rsc file.
In this case, the hello.rsc file should be :
module demo::hello
import IO;
void hello(){
println("Hello word");
}
That the reason why it does work when the hello.rsc in the current directory.

How to structure a simple Dart web app

For Dart starters I'm working on a simple web which consists of less than 10 classes. I'm totally confused as for how to organized them in files, folders (and packages? and libraries?).
Currently I have
web/
img/
*.png
styles/
main.css
index.html
main.dart
*.dart
All but one Dart file contain a single class. Imports are done through import 'a.dart'; (e.g. in b.dart).
This is obviously wrong because the Dart Editor complains about
The imported libraries 'c.dart' and 'd.dart' should not have the same
name ''
I went through the respective sections in pub documentation and read about possible app structures in the Polymer docs. I also looked at the structure of the pop_pop_win sample application that comes with Dart. It's all a bit overwhelming because there are so many variations, options and combinations.
If I were you, I would create libraries and imports like so:
awesomeLibrary.dart
library awesome;
part 'foo.dart';
part 'bar.dart';
foo.dart
part of awesome;
class Foo
{
static function Baz() {}
}
bar.dart
part of awesome;
class Bar
{
...
}
main.dart
import 'awesomeLibrary.dart';
void main()
{
Foo.Baz(); // Imported library function
}

How to make a function/class from an imported library accessible for all the libraries imported below in Dart?

Suppose I have this:
benchmark.dart:
library benchmark;
benchmark() {...}
app.dart:
import 'benchmark.dart'; // functions from this lib are now accessible in this file
export 'benchmark.dart'; // does this make them accessible in all files imported below?
import 'model.dart';
void main() {
doSomething();
}
model.dart
doSomething() {
benchmark(); // => Error, no such method, unless
// I import 'benchmark.dart' above in this file!
}
Is this the right behavior? How do I make benchmark accessible without importing the lib in model.dart?
You have to make the imports in every library you want to use other libraries.
What you might do is to join several files to one library using part 'model.dart'; (parent file) and part of app; (linked file).
Then the types and functions imported in the parent file are available in all files of this library.

How to reference another file in 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?
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.
Importing your own created libraries:
You will be importing the filename.dart and not the name of your library.
So if the name of your library is: myLib and it is saved in the file: someDartFile.dart you will have to
import 'someDartFile.dart';
If you have on Windows a library at: K:\SomeDir\someFile.dart you will need to write:
import '/K:/SomeDir/someFile.dart';
example:
import 'LibraryFile.dart'; //importing myLib
void main(){
//a class from myLib in the LibraryFile.dart file
var some = new SomeClassFromMyLibrary();
}
myLib in LibraryFile.dart:
library myLibrary;
import 'dart:math';
class SomeClassFromMyLibrary{
String _str = "this is some private String only to myLibrary";
String pubStr = "created instances of this class can access";
}
Here a full example.
//TestLib.dart
import 'LibFile.dart'; //SomeLibrary
void main() {
print("Hello, World!");
LibFile l = new LibFile();
print(l.publicString);//public
print(l.getPrivateString);//private
print(l.getMagicNumber); //42
}
//LibFile.dart
library SomeLibrary;
part 'LibFile2.dart';
class LibFile {
String _privateString = "private";
String publicString = "public";
String get getPrivateString => _privateString;
int get getMagicNumber => new LibFile2().number;
}
//LibFile2.dart
part of SomeLibrary;
class LibFile2 {
int number = 42;
}
Although i am answering very late, but the answer may help new developer.
Always use pubspec.yaml file in your dart package(application/library).
once you run pub get command it will add your local library in the dependencies list in .packages file.
Consider i have following project structure.
To refer to the content of greeting.dart in my main.dart file i should add the library as below
import 'package:my_project_name/greeting.dart'
Once imported we can use the content of greeting.dart file in our main.dart file.
Note: we have not used the actual path as you can see 'lib' directory is missing.
First make sure that's the name which you have mentioned in pubspec.yaml and the file you want to import are sharing the exact same name
example:
pubspec.yaml
name: flutter_wordpress_app
description: flutter wordpress app
...
....
// dirOne/dirTwo/greeting.dart
class FavArticleBloc {
// Your code goes here
}
import 'package:flutter_wordpress_app/dirOne/dirTwo/greeting.dart'
void main(){
var some = new FavArticleBloc();
}
But
in the main.dartyou don't need to specify
import 'package:flutter_wordpress_app
just do like below
import 'dirOne/dirTwo/greeting.dart

dart #import weirdness in dart editor

So I have two dart files -- One defines the entry-point Main() and the other is a class I've created. The Main file #imports dart:html and #sources my class. My class uses the dart:html namespace, and Dart Editor will display errors if I don't #import it. However, my class will fail to compile if I #import dart:html since the Main file already does, but compiles fine without the #import. Is there a way to appease the dart editor, or is this a known issue with how the dart editor resolves namespaces?
You should only do the import once and then source your program files from your main file. Something like this:
main.dart
#import("dart:html");
#source("program.dart");
main() {
var program = new Program();
program.run();
}
program.dart
class Program {
run() {
var elm = new Element.html("<p>hello world</p>");
document.body.nodes.add(elm);
}
}
should definitely work.

Resources