How to add a custom library in project Flutter? - flutter

I want to create a custom library and use it in Flutter project.
I created a custom library in project:
But I can't get pub it. (Error)
Because write_library_external depends on lib_math from path which doesn't exist (could not find package lib_math at "..\lib_math"), version solving failed.
pub get failed (66; Because write_library_external depends on lib_math from path which doesn't exist (could not find package lib_math at "..\lib_math"), version solving failed.)
This is my pubspec.yaml
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
lib_math:
path: /lib_math
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
Please help me! Thanks!

It looks like you added the wrong library path.
With your project, you should try changing the file at pubspec.yaml:
dependencies:
lib_math:
path: ../write_library_external/library/lib_math

Related

Error on line 18, column 5 of pubspec.yaml: A dependency may only have one source

I'm doing a flutter bootcamp course. The task was to add the english_words dependency but while implementing that I ran into an error in the dev_dependencies as it came from the code stub.
How can I fix this error?
name: xylophone
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
# Use Audioplayers dependency version 0.17.4
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/
As for error message, there was an indentation issue. I think you didn't save the pubspec.yaml file. Copy and paste the full snippet and save it then run flutter pub get or use the extension you've used
name: xylophone
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
english_words: ^4.0.0
cupertino_icons: ^0.1.2
# Use Audioplayers dependency version 0.17.4
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/
You can use the pubspec.yaml given by #yeasin Sheikh.. Also if you unfamiliar with using pubspec. The easiest way to add a package is to run
flutter pub add english_words
in terminal.
This will add the latest version of english_words package to your pubspec with proper indentation.

A problem occurred configuring project ':shared_preferences_linux'

When I add a dependency shared_preferences: ^0.5.8
Where:
Script 'D:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 362
What went wrong:
A problem occurred configuring project ':shared_preferences_linux'.
Could not find method implementation() for arguments [project ':path_provider_linux'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
pubspec.ymal
environment:
sdk: ">=2.1.0 <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
shared_preferences: ^0.5.8
dev_dependencies:
flutter_test:
sdk: flutter
You must show the pubspec.yaml file
he problem has been solved, The reason is that the SDK version is too low, v1,12.13+h9 update to 1.17.5

Unable to install new packages with pub command

The following is the excerpt from my pubspec.yaml file. I have added new dependency for shared preferences. when i do command pub get i get the error
Package secondapp requires SDK version >=2.7.0 <3.0.0 but the current SDK is 1.10.1.
I am able to resolve this by adding proper path to my dart installation. But after doing this, the flutter version error is coming.
My yaml file :
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <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.3
shared_preferences: ^0.5.6+3
dev_dependencies:
flutter_test:
I am unable to add the dependency. What settings am i missing?

Pubspec.yaml what this file does in Flutter

version: 1.0.0+1
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
sqflite: any
path_provider: any
intl: ^0.15.7
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
What things are written in it?
version: 1.0.0+1
The version of your application or package.
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Your application or package claims to support Dart SDK within this version range
dependencies:
flutter:
sdk: flutter
Your application or package depends on the flutter package which can be found in the SDK
sqflite: any
Your application or package depends on the package sqflite from https://pub.dartlang.org with no specific version constraint.
path_provider: any
intl: ^0.15.7
Your application or package depends on the package intl from https://pub.dartlang.org on any version 0.15.7 or later but before 0.16.0.
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
The ^ changes the meaning for versions 1.0.0 and later.
^0.15.7 means >=0.15.7 <0.16.0
^1.15.7 means >=1.15.7 <2.0.0
because for versions < 1.0.0 breaking changes are indicated by incrementing the middle number, while for >= 1.0.0 breaking changes are indicated by incrementing the first part of the version.
It is responsible for handling of importing images/fonts/third party packages which you want to include in your project.
As explained here on the Flutter site:
The pubspec file manages the assets and dependencies for a Flutter app.
More info can be found here.
To briefly explain, this file written in the YAML language, allows you to manage the pub packages you want to use in your flutter app.
From the dart page :
Every pub package needs some metadata so it can specify its dependencies. Pub packages that are shared with others also need to provide some other information so users can discover them. All of this metadata goes in the package’s pubspec: a file named pubspec.yaml that’s written in the YAML language.
So you will find all the required dependencies / fonts and images sources /sdk version in the pubspec.yaml

Flutter Error on line 6, column 5 of pubspec.yaml: A dependency may only have one source. sdk: flutter ^^^^^^^^^^^^^

I keep getting
Running "flutter packages get" in flutter_sportters...
Error on line 6, column 5 of pubspec.yaml: A dependency may only have one source.
sdk: flutter
^^^^^^^^^^^^^
When I run my app or Packages Get.
It worked perfectly fine before. Have no idea how to fix this.
Consider you are going to use this package "shared_preferences".
You will get this error in pubspec.yaml. If you did like below.
dependencies:
flutter:
sdk: flutter
shared_preferences: v0.4.2
Indention is important you are accidentally adding shared_preference package below flutter dependency. So the error "A dependency may only have one source"
Correct format below:
dependencies:
flutter:
sdk: flutter
shared_preferences: v0.4.2 #no indention
It throws an error because of Indention. It is important to maintain Indention while adding a dependency in flutter.
Before :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words: ^3.1.0
After :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words: ^3.1.0
Hope this will solve your error.
Happy coding!!!
Indentation !!!
Being newbees making silly mistakes. I have written the new dependency inside the flutter root as :
dependencies:
flutter:
sdk: flutter
sqflite:
Instead of
dependencies:
flutter:
sdk: flutter
sqflite:
Comment when you see the difference!!
I was trying to add assets folder to my project. I added it under dependencies which caused the error:
dependencies:
flutter:
sdk: flutter
assets:
- images/
It's not to be added under dependencies. Instead, add it under flutter:
flutter:
uses-material-design: true
assets:
- images/
You can define just like that in pubspec.yaml file
dependencies:
flutter:
sdk: flutter
image_picker: 0.4.1
and flutter packages get you can call via terminal or if you are using Android Studio than options availbale above when you edit pubspec.yaml file.
In terminal go to your project directory and then enter flutter packages get
I solve My problem:
Here my first Code
dependencies
flutter:
sdk: flutter
webfeed: ^0.4.2
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
after I change it
dependencies:
flutter:
sdk: flutter
webfeed: ^0.4.2
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
I thing you must to put your package name in the same number colunm of flutter: and cupertino_icons:
Check indentations as flutter package in pubspec.yaml takes single dependency source.
make sure, the name of your project is not same as any package ,
this too cause for this error
open your pubspec.yaml file
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
country_code_picker: ^1.0.4
Other answers are correct. I would also like to add that I was trying to add shared_preferences and even though my indentation was correct, I was still getting the error mentioned in the question. To resolve it I changed the name of my project as it was also shared preferences
I kept getting this error no matter what I did, including commenting out the new lines. Finally, I put them back in with the correct indentation and ran the program. The problem magically resolved itself. However, the indentation is critical.
I am getting this error due to duplication of a package in dependencies
i.e:
dependencies:
flutter:
sdk: flutter
**cupertino_icons: ^0.1.2**
english_words: ^3.1.5
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
I solved this error by removing duplication then Flutter clean and then Flutter Packages Get.
Its due to indentation: check out correct way
name: flutter_app
description: A new Flutter application.
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
http: "0.11.3+17"
autocomplete_textfield: ^1.6.4
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
I ran into the same problem turns out the name of my app and the package name were same which resulted into this error.
name of the app scrapy and the dependency was also scrapy...
Changed the name and it worked.
After checking all other solutions if the problem still persists
make sure whether the specified library is installed correctly or not. This error also encounter when the library is not installed correctly.