Dart import a package you have created - dart

I have some code that i want to use between two projects. I would like to break this out into a package and import it into other projects/packages that need it. When i try to import a locally created package, I get a "The target URI doesnt exist" error. What do i need to do? Is there a path environment variable that dart checks to find local packages? Do i need to publish my package to pub.dev and use "dart pub add"? (I really would not prefer this)
Thanks in advance

The dependencies section of pubspec.yaml supports, along with the pub.dev and Git options, a path option.
Path dependencies are documented on the Dart website.
For example:
dependencies:
hosted_package: ^1.0.0
local_package:
path: ../my_local_package

Related

How to create pubspec.yaml by command?

I am learning Dart (2.9.3). I use pub global activate webdev command.
I think it will work like npm in Node.js and create pubspec.yaml automatically, but it didn't.
Do I need to add more switches (like --save) to pub global?
Thank you.
The pub global activate webdev command activates webdev package to use on your computer. This package is
A command-line tool for developing and deploying web applications with Dart.
As author wrote in usage section
webdev provides two commands: serve and build.
But neither of both creates pubspec.yaml file.
If you want to generate a project, you can use stagehand. But there is no option to generate customized pubspec.yaml file. Dart must know which packages to use in a particular project, and only this file can provide those informations.

Dart pub get command is not working properly when i try to pull my dependencies

I have my project in github. I already created it like a library (Dart). Now I am trying to use my library in one of my project.
After i add all the necessary comments to get it from my git source, pub get is giving the following error,
Warning: Package <library_name> does not have a "lib" directory so you will not be able to import any libraries from it.
Got dependencies!
Any clue???
Thanks,
Tham
When importing libraries, the files you import come from the lib folder.
Eg:
import 'package:danny/danny.dart';
will import the file lib/danny.dart from the danny package.
If the package doesn't have a lib folder; this means you can't possibly import it. If you're importing directly from a Git repo; you need to ensure the lib folder is a top-level folder.

Dart web application can not import local package

I have developed a simple library (called picasawebalbums) that I wish to import in another web application.
I have added the the following to pubspect.yaml
picasawebalbums:
path: C:\Users\hangs_000\Documents\GitHub\PicasaWebAlbums
But the package does not appear under the 'packages' directory and of course my dart code cannot import any of the classes.
I feel that I have missed out a step somewhere.
Try pub get --verbose or pub upgrade --verbose from command line in your application directory and look at the output. You can try without --verbose first. This prints much less but may offer a hint anywhere.

Failed to load package in Dart

When I try to import a package with the syntax import 'package:markdown/markdown.dart';, I get no error in Dart Editor but when I run the dart application, the debugger shows me the message:
An error occurred loading file: package:markdown/markdown.dart
Failed to load resource
chrome-extension://gfjabgeipkcfopofkhjimepepnomcidk/dart/packages/markdown/markdown.dart
But when I write the whole path (import "../../packages/markdown/markdown.dart";) everything works fine. I cannot understand why the syntax package: doesn't work in my code though it works in Dart Editor's own examples.
You can see the Chrome app architecture below (I'm loading a package from translator.dart):
You should have the package added as a dependency in pubspec.yaml (which I assume you do).
Also try running following:
delete packages folder
delete pubspec.lock
run pub get to fetch the
dependencies again.
I think in your md_to_html folder you need a link to the 'packages' directory for the shortform to work. I seem to remember this happening when the workspace is built but I'm not sure now which command triggers it, have a look at the current pub docs.

What does Package 'unittest' is depended on from both sources 'sdk' and 'hosted' mean?

I have a pubspec.yaml file like this:
name: My App
dependencies:
unittest: { sdk: unittest }
json_object:
git:
url: git://github.com/chrisbu/dartwatch-JsonObject.git
(I'm just using JsonObject as an example here)
When I run pub install I get this error:
Package 'unittest' is depended on from both sources 'sdk' and 'hosted'
What does this mean and how can I resolve it?
Pub, the Dart package manager, identifies packages by name as well as where they come from. If pub detects two packages with the same name, but come from two different sources, it will throw an error like "Package foo is dependend on from both sources 'sdk' and 'hosted'"
To resolve this, you need to ensure all of your dependencies refer to the same package with the same source.
The right solution is for every package to stop using the sdk sources, as all of the SDK packages are now hosted in pub.dartlang.org.
You should change:
dependencies:
unittest: { sdk: test }
Into this:
dependencies:
unittest: any
The any means "any version from pub.dartlang.org"
The following packages are now in pub, their new canonical home:
args
http
intl
logging
meta
oauth2
unittest
webdriver
If you use any of the above packages, please use foo: any instead of {sdk: foo} in your pubspec.yaml file.
Now, of course you as a developer can update your own pubspec.yaml, but you may not be able to control your 3rd party dependencies. I recommend that you contact your package's author via email (which you can get from pub.dartlang.org) as ask them to update to using hosted packages like unittest.
See more at http://news.dartlang.org/2012/12/sdk-packages-now-available-on-pub.html

Resources