how to use local flutter package in another flutter application?

DartFlutterFlutter Dependencies

Dart Problem Overview

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

Dart Solutions

Solution 1 - Dart

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

Use local dependency

         sdk: flutter
         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.

Solution 2 - Dart

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:

    path: ../your_package/

Solution 3 - Dart

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:

       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.

Solution 4 - Dart

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

    sdk: flutter

  #  advance_pdf_viewer: ^2.0.0
    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!


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbehzad besharatiView Question on Stackoverflow
Solution 1 - Dartbehzad besharatiView Answer on Stackoverflow
Solution 2 - DartDeepak swainView Answer on Stackoverflow
Solution 3 - DartGraSimView Answer on Stackoverflow
Solution 4 - DartPurushottam DikeView Answer on Stackoverflow