Reputation: 6920
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
Upvotes: 204
Views: 121334
Reputation: 4230
For the full process:
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
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
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.
Upvotes: 21
Reputation: 401
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
Download zip file and extract it in pdf_viewer/
Make sure pdf_viewer/
has all files including lib
, Android
, iOS
and all related files.
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
.
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
viewer.dart
file to achieve desired changes.Hope you and other got some information from this finding!
Upvotes: 8
Reputation: 6920
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.
Upvotes: 336
Reputation: 3700
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/
Upvotes: 45