How do I import a library from the lib folder in my web app?

81 views
Skip to first unread message

Danny Tuppeny

unread,
Aug 20, 2014, 12:27:22 PM8/20/14
to mi...@dartlang.org
(Posted to StackOverflow too)

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?

Matthew Butler

unread,
Aug 20, 2014, 12:47:09 PM8/20/14
to mi...@dartlang.org
from outside of lib/ use:

import 'package:package_name/my_library.dart';

Make sure that package_name matches the name of your package as defined in the pubspec. Case sensitive.

=)

Matt

Danny Tuppeny

unread,
Aug 20, 2014, 12:50:03 PM8/20/14
to mi...@dartlang.org
Thanks; I got the same answer on SO. Makes sense now (after seeing the link in packages/)! :)


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

Reply all
Reply to author
Forward
0 new messages