How to get version of pub package (mentioned in pubspec) inside package - dart

Say if this is my pubspec.yaml
name: my_pub
description: My Pub
version: 0.0.1
publish_to: none
How do I get the version name/number of my_pub inside that package. Is there any way to get it?
Or should I store it as a const inside lib? In that how do i make sure both the verison name (in pubspec and inside package) be in sync.

Related

Use pub global activate --source path from other local dart project

I am trying this, and it works great for terminal applications to reference $HOME/.pub-cache/bin
Can I use an activated local package from other local projects?
Steps:
1. pub global activate --source path . From package project
2. Then how to use this in another project pubspec.yaml dependencies: section?
I know I can use path, but this is much better for dynamic reference reasons.
If I add this in pubspect.yaml dependencies in the project using cli2 package:
dependencies:
cli2: 0.0.0 # name of: pub global activate --source path .
I get this error:
Because usecli2 depends on cli2 any which doesn't exist (could not find package cli2 at https://pub.dartlang.org), version solving failed.
pub global activate is to install Dart scripts from bin/ of a package as command line executables and is completely unrelated to adding dependencies to a project.
You can use relative paths to add local dependencies:
dependencies:
cli2
path: ../cli2

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

What does Package 'unittest' is depended on from both sources 'sdk' and 'hosted' mean?

I have a pubspec.yaml file like this:
name: My App
dependencies:
unittest: { sdk: unittest }
json_object:
git:
url: git://github.com/chrisbu/dartwatch-JsonObject.git
(I'm just using JsonObject as an example here)
When I run pub install I get this error:
Package 'unittest' is depended on from both sources 'sdk' and 'hosted'
What does this mean and how can I resolve it?
Pub, the Dart package manager, identifies packages by name as well as where they come from. If pub detects two packages with the same name, but come from two different sources, it will throw an error like "Package foo is dependend on from both sources 'sdk' and 'hosted'"
To resolve this, you need to ensure all of your dependencies refer to the same package with the same source.
The right solution is for every package to stop using the sdk sources, as all of the SDK packages are now hosted in pub.dartlang.org.
You should change:
dependencies:
unittest: { sdk: test }
Into this:
dependencies:
unittest: any
The any means "any version from pub.dartlang.org"
The following packages are now in pub, their new canonical home:
args
http
intl
logging
meta
oauth2
unittest
webdriver
If you use any of the above packages, please use foo: any instead of {sdk: foo} in your pubspec.yaml file.
Now, of course you as a developer can update your own pubspec.yaml, but you may not be able to control your 3rd party dependencies. I recommend that you contact your package's author via email (which you can get from pub.dartlang.org) as ask them to update to using hosted packages like unittest.
See more at http://news.dartlang.org/2012/12/sdk-packages-now-available-on-pub.html

Pub install fail, Package "html5lib" doesn't have a pubspec.yaml file

I've created a simple project. This is my pubspec.yaml
name: testapp
description: test application
dependencies:
html5lib: 0.0.12
And now i get this error
Pub install fail, Resolving dependencies...
Package "html5lib" doesn't have a pubspec.yaml file.
I'm guessing you are on windows? Dart seems to setup shortcuts (hard dir links?) between the packages folder in a project and where the packages are stored. So if you delete your packages dir from within Eclipse, it will trash the folder that stores the actual package.
On Windows 7, the folder is:
C:\Users\[user]\AppData\Roaming\Pub\
Go ahead and delete its contents and run pub install again.
If you start getting errors about UnitTest or other core libraries, you may need to re-download the Dark-SDK (or dart editor) and replace it.

pub install does not work in Dart

I am trying to install glmatrix using pub install. My OS is Windows 7 and I have Git installed.
Here is my pubspec.yaml
name: WebGLTut1
description: A sample application
dependencies:
glmatrix:
git: git://github.com/pjako/glmatrix.dart.git
When I run pub install I get the following error:
Running pub install ... Pub install fail, FormatException: Could not
parse "git://github.com/pjako/glmatrix.dart.git".".
I googled a while to find a solution but couldn't. Please help.
UPDATE#1:
I found the problem. There has to be a TAB/Space when specifying git: url in pubsec.yaml as follows.
glmatrix:
git: git://github.com/pjako/glmatrix.dart.git
This solved the previouse problem of FormatException. But now when I run pub install again it throws theis error.
Running pub install ... Pub install fail, Git failed.
As you identified you update, yaml files are whitespace sensitive; that's why you don't have to use lots of curly braces like json. :)
I see a few of possible problems here:
First
Glmatrix doesn't appear to have its own pubspec defined. Libraries participating in the package system much declare a pubspec file with the name property defined. The name is important because pub uses it when you declare your dependencies. Let's say you had a project called toast with the following simple pubspec:
name: butter
version: 0.0.0
Then you would need to declare your dependency to that project like so:
dependencies:
butter:
git: git://github.com/foo/toast.git
Have the project owner make the changes or fork it yourself, add the pubspec, and then reference your fork in your pubspec.
Second
Glmatrix is does not conform to the pubspec package layout conventions (your own projects should also conform to this). Although I believe that pub may handle non-conforming layouts currently, it may not in the future. So again, you can either ask the project owner to fix, or fork and fix it yourself.
Third
Since you are on windows, make sure git is in your system PATH variable. If you are using the bash shell, git will work, but it's not going to work outside of bash (like from Dart editor) until you add it to your PATH.

Resources