Dart: importing local libraries with git
Published November 21, 2012
We have a couple of ways to manage application’s dependencies with pub, we can
- import a hosted package
- clone a git repository
Just to be clear, hosted packages are not hosted on a general server, they are hosted on a pub server; since setting up our own local pub server or taking advantage of pub-cache could be really tricky, a safe and fast way to import local packages is doing it via the git system.
Let’s see how to create an application, my_app, that imports a local library, my_lib.
Assume that my_lib and my_app folders are in user’s home directory, e.g.
Linux: /home/bob/my_lib OS X: /Users/bob/my_lib Win7 C:\Users\bob\my_lib
- Create my_lib folder
- Create lib folder within my_lib
- Create my_lib.dart within lib folder and pubspec.yaml at top of my_lib folder
Here’s the directory tree of *my_lib:
./my_lib/ ./my_lib/lib/my_lib.dart ./my_lib/pubspec.yaml
Content of my_lib.dart:
Content of pubspec.yaml:
Now we have to “convert” my_lib into a git repository; change to my_lib and type:
We’re done with my_lib, let’s switch to my_app
- Create my_app folder
- Create bin folder within my_app
- Create my_app.dart within bin
- Create pubspec.yaml at the top of my_app folder
Content of my_app.dart:
And now set content of pubspec.yaml to:
Remember to change git: argument accordingly to current operating system
We’re almost done. Last step is running
pub install in my_app folder.
Output should return:
Some symlinks, packages, have been created in my_app folder
Now we’re ready to run our awesome application: