how to use local flutter package in another flutter application? - dart

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

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.

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/

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.

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
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!

Related

Missing kernel_blob.bin file in Release folder when build Flutter code into frameworks

I want to create a Flutter module, then I will embbed it to native iOS project.
I'm following Option B in this document of Flutter to create a module.
As in the document, I executed this command:
flutter build ios-framework --output=some/path/MyApp/Flutter/ to generate Flutter code into frameworks.
But when I imported App.xcframework and Flutter.xcframework (from same folder Release), I got this error:
After researched, I followed this document of Flutter, I updated Thin Binary script and add Other Linker Flags.
But when I build Flutter's code into frameworks, only App.xcframework in Debug folder have kernel_blob.bin file.
This is my flutter config:
How to make App.xcframework in Release folder have kernel_blob.bin file?
Thank in advance for your help

Plugin.h file not found: flutter and xcode

When I try and run my project in xcode, I am getting the following error:
'auto_orientation/AutoOrientationPlugin.h' file not found.
If I remove this plugin, it will then go to the next plugin and fail on the .h file for that plugin and so on for every single plugin.
Previously this issue only used to happen when I attempted to make an archive, but it would always run without issues. Now it will not even run.
There are many threads about this and I have run through these but have so far been unable to find any suggestion that is working.
Pubspec.yaml file, as requested:
name: app_name
description: My application.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 6.0.0
environment:
sdk: ">=2.3.0-dev.0.5 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: 0.1.2
firebase_admob: 0.5.5
screen: 0.0.5
image_picker: 0.6.1
firebase_auth: 0.11.1+7
shared_preferences: 0.4.3
rflutter_alert: 1.0.2
url_launcher: 5.0.3
in_app_purchase: 0.2.0
firebase_dynamic_links: 0.4.0+4
app_review: 1.1.0+1
auto_orientation: 1.0.4
vibration: 1.2.1
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
assets:
- lib/images/
- lib/images/example1.png
- lib/images/example2.png
- lib/images/example3.png
uses-material-design: true
Screenshot for Cenk:
you definitely do not need to wipe out your whole ios/ folder.
1) flutter cleancause why not
2) rm ios/Podfile.lock
3) rm -rf ios/Pods
4) rm -rf ~/.pub-cache/hosted/pub.dartlang.org/ (or wherever you set this up to live)
5) flutter packages get
then run/build your app however you normally do it.
Sometimes I also get ios compilation error in my project working properly, I create a clean project with the same name, copy my codes there and it gets better.
I suggest you try if you can't find a solution.
I have had such issues before. The error was because I deleted a package dependency from my pubspec.yaml. As we know deleting a package as such will remove all the flutter files it downloaded form pub.dev but forgot to delete the permission and method I added into Info.plist and AppDelegate.swift manually as dependencies for that package.
When Xcode see this methods in the AppDelegate.switf, it searches for the required files but it can't find them because the package was deleted from pubspec.yaml.
So cross-check if the content of those 2 files matches the packages that require them.
Hope it helps.
Edit
Based on your pubspec.yaml, they are some keys that need to be added to your AndroidManifest and info.plist files respectively. I assume you haven't done that from your comment.
Locations
AndroidManifist is found in android/app/src/main/AndroidManifiest.xml
info.plist is found in iso/Runner/Info.plist
For the firebase_admob package you need to add the following to your AndroidManifest
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[ADMOB_APP_ID]"/>
Where [ADMOD_APP_ID] is your App ID. Check firebase_admod to see how to initialize an app ID and how to initialize the plugin
Then you need to add the following to your info.plist
<key>GADApplicationIdentifier</key>
<string>[ADMOB_APP_ID]</string>
For screen 0.0.5 you need to add the following permission to your 'AndroidManifest`
<uses-permission android:name="android.permission.WAKE_LOCK" />
For image_picker: 0.6.1, you also need some permission added to the info.plist.
Add the following keys to your Info.plist file, located in /ios/Runner/Info.plist:
NSPhotoLibraryUsageDescription - describe why your app needs
permission for the photo library. This is called Privacy - Photo
Library Usage Description in the visual editor.
NSCameraUsageDescription - describe why your app needs access to the
camera. This is called Privacy - Camera Usage Description in the visual editor.
NSMicrophoneUsageDescription - describe why your app needs access to
the microphone if you intend to record videos. This is called Privacy
Microphone Usage Description in the visual editor.
Android No configuration required - the plugin should work out of the box
Not to be redundant, you need to browse you the package in you pubspec.yaml pub.dev page and follow the instruction to add all the dependencies for the packages to work.
I hope you project compiles now!
I guess you have some files missing for some reason.
I hope this steps maybe solve it:
backup your flutter project folder
delete "ios" folder from your your flutter project
run flutter create -i swift , this command will recreate new 'ios' folder
run your flutter project
In the end I found a fix for this in another forum:
Seems like by opening up the project by double clicking Runner.xcworkspace instead of Runner.xcodeproj xcode can finally figure out where everything is.
I tried this and it literally works. Now my app will run with a new error (because of course):
malloc: can't allocate region securely
But I will make a new question for that one.

Using a static iOS library in a NativeScript 5.4 plugin

NativeScript 5.4.2
tns-core-modules 5.4.3
tns-ios 5.4.2
Xcode 10.2.1
I am attempting to create a plugin for this background geolocation library for iOS: https://github.com/Yermo/background-geolocation-ios
The project produces a static library, libBackgroundGeolocation.a.
I have read the documentation on how to use a static iOS library: https://docs.nativescript.org/plugins/Use-Native-iOS-Libraries#static-libraries
I have read about the required directory structure: https://docs.nativescript.org/plugins/plugin-reference#directory-structure
My plugin is based on the official plugin seed, so I have a demo directory and a src directory containing my plugin code and a platforms/ios directory under that.
I have copied the BackgroundGeolocation.a file into src/platforms/ios.
I have created a src/platforms/ios/include/BackgroundGeolocation directory and copied the public header files from the project into that directory.
I notice that src/platforms/ios/include/BackgroundGeolocation/module.modulemap file has been created.
In the plugin directory I can successfully run 'npm run build'.
However, from the demo directory if I run 'tns build ios' I consistently get a "ld: Library not found for -lBackgroundGeolocation" error.
If I rename the library to libBackgroundGeolocation.a I get the "ENOENT: no such file or directory, scandir '.....ios/include/libBackgroundGeolocation" so I surmise it's at least recognizing that the library is there.
I thought that maybe it was a path problem so I tried setting the library include path explicitly in build.xcconfig using the full path to the platforms/ios directory.
No joy. It still doesn't find the library but if I misspell the name of the directory in the -L in build.xcconfig it flags a directory not found error.
I then thought that maybe it had to do with the targets in the library so I followed the recipe here Build fat static library (device + simulator) using Xcode and SDK 4+ to include multiple targets.
Still no joy.
I suspect I am missing something fundamental.
What am I doing wrong?
Is there a working recent working sample somewhere of how to wrap a static iOS library as a NativeScript plugin?
It turns out adding
LIBRARY_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../node_modules/<projectname>/platforms/ios
to the src/platforms/ios/build.xcconfig file resolved this issue.

Extract jar from aar for cordova plugin, Class not found error at runtime

I have an "aar" file that was generated using gradle with
apply plugin: 'com.android.library'
Now I want to use this aar library to create a cordova plugin. I unzip the aar file, get a "classes.jar" and put the jar into cordova's "libs" directory. My cordova project compiles without any problem, I can import and use classes in the jar file. But it crushed at run time with the following error
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/pkg/R$raw;
Package "com.example.pkg" is in my "classes.jar", but there is no class call "R$raw". What did I miss?
AAR files bundle not only source but also the libraries self contained Android resources. Take a look at the contents of an AAR bundle.
It appears the original AAR file you had contained some raw resources which are not included in the jar. You cannot package Android resources in a .jar file so your application is crashing when it needs to access the ID's generated for resources.
You can check this by opening up the original AAR file (it is simply a zip archive). Take a look under the res folder for any resources that are needed by the library.
Yes, extracting classes.jar is not going to work and it will throw runtimeexceptions...
What i did to solve the problem is:
After cordova project creation, adding platform, adding plugin is done.
Lets say the aar file are copied in libs folder. ( assume file name is cards.aar )
then in app build.gradle specify following and click sync project with Gradle files.
repositories { flatDir { dirs 'libs' } }
dependencies { compile(name:'cards', ext:'aar') }
It works for me!! but one drawback of this solution is .. you need to open the project from platform folder on Android Studio and add the above lines to your build.gradle

Dart Package Management via dart2js

I'm learning Dart and its dependency manager pub and am having a tough time seeing the "forest through the trees" here.
Say I want to use Polymer.dart in my project. So, in my project root, I create the following pubspec.yaml:
name: test_dart
description: A sample web application
dependencies:
browser: any
polymer: ">=0.9.0 <0.10.0"
I then run pub get, which goes to the pub repo and fetches the browser and polymer dependencies that I've specified. It then creates a packages directory in my project root, which now means I have a project that looks like:
MyDartProject/
pubspec.yaml
myapp.dart
packages/
browser/
...
...all the packages that ship with Polymer
Now I start coding my Dart web app (myapp.dart), which will references various Polymer and browser types/functions/etc. in its source code.
When I'm all done, I want to create a JavaScript file called myapp.js.
According to the dart2js docs, I need to run something like:
dart2js --out=myapp.js --package-root=??? myapp.dart
How do I include all the browser & polymer packages on the buildpath?
There is a "pub build" option now.
http://pub.dartlang.org/doc/pub-build.html
Use pub build when you’re ready to deploy your web app. When you run
pub build, it generates the assets for the current package and all of
its dependencies, putting them into a new directory named build.
$ cd ~/dart/helloworld
$ pub build
Building helloworld......
Built 5 files!
If the build directory already exists, pub build deletes it and then creates it again.
That should do everything you are after here. You can also launch it from the IDE by right clicking on the pubspec.yaml file and choose "pub build"
EDIT: You should also see the links in zoechi's answer.
If you run dart2js from your MyDartProject directory you don't have to provide --package-root parameter.
An alternative way is running pub build. If you use Polymer you need to add a transformers section.
see also
How to deploy a Dart Polymer app to Javascript using dart2js
How do I pass multiple entry_points to polymer transformer in pubspec.yaml?

Resources