My folder structure looks like this:
lib\
my_library.dart
web\
index.html
main.dart
pubspec.yaml
In main.dart
, I'm currently importing my_library.dart
(which has library my_library
at the top) by doing:
import '../lib/my_library.dart';
This works fine. However, it feels a bit fragile to have this relative path. I read in the Pub Package Layout Conventions that I can probably use import 'package:blah'
; it says:
When you use libraries from within your own package, even code in src, you can (and should) still use "package:" to import them
So, I tried changing my code to this:
import 'package:my_library.dart';
However, in Chrome Dev Editor I get:
Target or URI does not exist: 'package:grid_data.dart'
and in Dartium I get:
GET http:// 127.0.0.1:51792/MyProject/web/packages/my_library.dart 404 (Not Found)
What's the correct way to import something from my lib
folder from other dart files in my project?
You are missing the package name
import 'package:my_project/my_library.dart';
The package:
part in your import statement references the packages folder in your my_project
package (the folder that contains the pubspec.yaml
. The remainder of the import statement is the path to the file you want to import.
You can browse through the folders and files in this packages folder in DartEditor or a file manager to see exactly how the files are organized.
This folder also contains a symlink named my_project
which points to your my_project/lib
folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With