how to use local flutter package in another flutter application?
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
Solution 1:
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.
Solution 2:
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/
Solution 3:
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 samename:
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.
Solution 4:
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 includinglib
,Android
,iOS
and all related files. -
Copy your
pdf_viewer
folder in your project directory for example my project isinvoice_viewer
so it's root directory have all folders such aslib
, Android,iOS
etc. copy it in this root directory along withlib
,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
- 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!