Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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.

like image 783
Tino Avatar asked Jun 07 '18 04:06

Tino


People also ask

What is the difference between path and relative path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What is the difference between a full and relative path name?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

What is the difference between relative path and absolute path with example?

For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string. A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.

Is absolute path or relative path better?

Using relative paths allows you to construct your site offline and fully test it before uploading it. An absolute path refers to a file on the Internet using its full URL. Absolute paths tell the browser precisely where to go. Absolute paths are easier to use and understand.


1 Answers

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?
like image 171
Günter Zöchbauer Avatar answered Oct 14 '22 09:10

Günter Zöchbauer