204

How to use local flutter package in another flutter application?

I created a package using following command:

flutter create --template=package my_new_package

and then in my application source code => main.dart

import "package:my_new_package/my_new_package.dart" // can not find the package
1
  • 1
    I have the same trouble. Though the app ran, the error, “The URI doesn’t exists” occurred. I executed “flutter packages get”, but the result was the same.
    – hrsma2i
    Commented Dec 17, 2018 at 10:46

4 Answers 4

338

Find this file in your flutter application => pubspec.yaml

Use local dependency

    dependencies:
       flutter:
         sdk: flutter
       my_new_package:
         path: ./my_new_package

Note: The ./my_new_package above means that the my_new_package directory containing the pubspec.yaml for the package is a sub-directory of the app.

If you have the package as a directory at the same level as the app, in other words one level higher up in the directory tree, you can use ../my_new_package (note the double dot) or a full path to the package directory.

7
  • 110
    Caution: There is a trap here that I would like to caution people against. Do not create a folder in your flutter root named packages and put packages in there. packages folder is removed on build.
    – TheGwa
    Commented Sep 14, 2018 at 21:05
  • 3
    @SacWebDeveloper the above answer is correct and without fault. My comment is just a caution. If you have project specific packages, the naive approach would be to put them in a /packages directory at the root of your project at the same level as /android, /ios, /tests etc. At the time of writing /packages was overwritten during build and would nuke your packages. Have not tested for some time.
    – TheGwa
    Commented Feb 22, 2019 at 6:40
  • 2
    The official documentation link is here, for package and plugin.
    – Tokenyet
    Commented May 1, 2019 at 14:47
  • 4
    @TheGwa Oh. That is useful info. Thanks. Maybe call it something like plugins or add them to lib/packages? Perhaps the community should come up with a convention. Commented Jul 11, 2019 at 1:00
  • 18
    It does seem that the /packages is no longer overwritten during build and the official documentation mentions no limitation of putting files in a directory named that way. I believe the warning no longer applies.
    – Rob
    Commented Oct 9, 2020 at 16:25
45

Path dependency: A Flutter app can depend on a plugin via a file system path: dependency. The path can be either relative or absolute. For example, to depend on a plugin plugin1 located in a directory next to the app, use the following syntax:

dependencies:
  plugin1:
    path: ../your_package/
2
  • This was useful for me when i wanted to depend a plugin to a plugin in the same path.
    – RaSha
    Commented Feb 1, 2021 at 6:06
  • To highlight again, your app and the package should be in same directory Commented Mar 21, 2021 at 9:55
21

For the full process:

  1. Download the code for the plugin you want to use and place it at the "same" level as your flutter project directory

     -- plugin-name  
     -- your flutter directory -- lib
                               -- android
                               -- ios etc etc
    
  2. Add the plugin path to pubspec.yaml. *If you are unsure of the correct plugin name to use, look at the name: attribute in the plugin's pubspec.yaml file. The plugin directory must also be saved with the same name:

    dependencies:
     plugin-name:
       path: ../plugin-name
    
  3. Run Pub get and you can import just like any other plugin. Only difference is when you click on any of the plugin classes during development, it will point to the local file.

*Note: Also delete the "example" folder that comes with the package. You don't need it and it will be a pain to maintain during project upgrades.

8

Hey I had same problem once I started using flutter. I have implemented example of pdf_view plugin in my app for making changes for Shimmer effect instead of CircularProgressIndicator().

Some Knowledge

You can edit plugins which are get by flutter pub get but they might be replaced when you create app bundle by flutter.

Now your answer with sample plugin suppose take an example of advance_pdf_viewer GitHub Link

  1. Download zip file and extract it in pdf_viewer/

  2. Make sure pdf_viewer/ has all files including lib, Android, iOS and all related files.

  3. Copy your pdf_viewer folder in your project directory for example my project is invoice_viewer so it's root directory have all folders such as lib, Android, iOS etc. copy it in this root directory along with lib, Android, iOS.

  4. Now open your pubsec.yaml and write code as follows

dependencies:
  flutter:
    sdk: flutter

  #  advance_pdf_viewer: ^2.0.0
  advance_pdf_viewer:
    path: ./pdf_viewer
  1. In comment I have replaced server version with local one and make changes in plugin's viewer.dart file to achieve desired changes.

Hope you and other got some information from this finding!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.