I am not able to use other packages in dependencies in flutter - flutter

I have just started using flutter and I am using flutter packages like material.dart , cupertino.dart but when it come to use other packages like image_picker, english_words, I am not able to use it and I had declared packages with versions in dependencies in pubsec.yaml file. After when I write the import package name in the main file it shows a red line under it and the folder was also not created on the left side.

It seems the packages that were declared as dependencies in pubspec.yaml files are not yet downloaded.
Try running: flutter packages get from your project's root directory.
Sometimes it doesn't work even after the above command is executed, in such case close current project and reopen it.

Related

flutter pubspec.yaml deleted after updating

Hello experts I am facing very big issue I am made application in 2021 on flutter my application is still live on play store now I need update it, problem is that I updated my android studio flutter version 3.0. Now opening my old project which was made in 2021 after opening it my pubspec.yaml file is missing in the project, I don't no how to recover that file or repair it. They have a lot of dependencies included there is pubspec.lock file available but not pubspec.yaml please please help me i am in big check screen shot below
if i use flutter create command i am getting this errors
You can create a pubspec.yaml file on root directory or open the bottom terminal and try flutter create . it will provide default structure. Now to get pub package, check the import section and add on pubspec.yaml
If you have a backup then paste the pubspec.yaml file from it. Else take a backup of the project. Run flutter create . in the root directory. It will create the pubspec file in the project. Click on configure dart and add the flutter path then run the project. It will throw errors for the packages used manually enter packages in pubspec or you can try flutter pub add <packagename>

Is it possible to add the latest version of a dependency to pubspec.yaml without finding it myself?

Lets say I am starting a new dart project. I have a few packages on pub.dev I know that I want to use, and I want to use the most recent version of these packages.
Right now, I open my browser and go to pub.dev and find each package, then check the Versions or Installing tab to find out what the latest version is so I can put that in my pubspec.yaml. Then I can run pub get
Using npm, you can run npm install <package> and npm will automatically fetch the latest version of the package and add it to package.json
Is there a pub command, syntax for pubspec.yaml, or something else that will make pub resolve the latest version of a package and use it in my project, without having to manually look up the latest version?
Normally in Flutter, the packages which you want to use need to be added to pubspec.yaml and run pub get. But there are also some IDE plugins like npm.
For example, Pubspec Assist is using on Visual Studio, you can add or update your dependencies without going pub.dev. But you have to know which packages do you want to use at least for a time.
https://marketplace.visualstudio.com/items?itemName=jeroen-meijer.pubspec-assist
Simply enter the following command in the terminal
flutter pub add provider
Of course, where it says provider, put the package you want to install.
The package with the latest version is automatically added to your pubspec.yaml.
Otherwise you can always do what #jamesdlin said and leave the version field empty.

Reference Local Path Package in Dart-Web Application

I sucessfully split up a flutter mobile application in to packages. One package is referenced by the flutter mobile app by using the following in the pubspec.yaml:
app:
path: /path/app
The fultter app builds and runs just fine and no problem is reported by the IDE (visual code) when it does the 'pub get'
When I do the same thing in the web-project the IDE complains on 'pub get' with:
Error on line 8, column 5 of pubspec.yaml: A version constraint must be a string.
When trying to build web project by issuing the command:
webdev build
I get the following error:
build_web_compilers:entrypoint on web/main.dart (cached): Unable to find modules for some sources, this is usually the result of either a
bad import, a missing dependency in a package (or possibly a dev_dependency
needs to move to a real dependency), or a build failure (if importing a
generated file).
I even tried to use Windows native top level path (C:\path\package) but without a changed outcome. It seems that webdev does not support local path packages like described by the official PackageDependencies | Dart manual page.
When using shorthand and creating the web project it does not create a dependencies section in the pubspec.yaml file. I added another dependency (package) right under the environment section where the SDK is also configured.
It does not complained about the package dependency being under environment but it complains about a package dependency that has no version but a path.
I only noticed when I used a symlink to add the lib's source folder (lib) directly to the web project as it kept complaining about not finding this said package dependency.
Checking the packages file the package was not mentioned and in the pubspec.lock file the package was listed under the 'sdks:' section.
By creating a 'dependencies:' section and putting both dependencies under it, it now works as intended... . Case closed!

Flutter web cannot use packages from mobile project

A few months ago I made an android application using flutter. The application requires various dependencies such as intl, cached network image, stopper, carousel_slider, etc.
Then right now I want to convert the project to flutter_web. Here are the steps that I did:
clone github web flutter
I run the command "flutter packages pub global activate webdev"
Then I edited pubspec.yaml and I adjusted it to pubspec.yaml in the previous project (android project)
Here is the contents of pubspec.yaml on the web flutter project:
name: flutter_web.examples.hello_world
environment:
  # You must be using Flutter> = 1.5.0 or Dart> = 2.3.0
  sdk: '> = 2.3.0-dev.0.1 <3.0.0'
dependencies:
  flutter_web: any
  flutter_web_ui: any
  stopper: ^ 1.0.1
dev_dependencies:
  build_runner: ^ 1.4.0
  build_web_compilers: ^ 2.0.0
dependency_overrides:
  flutter_web:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages / flutter_web
  flutter_web_ui:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages / flutter_web_ui
  provider:
    git:
      url: https://github.com/kevmoo/provider
      ref: flutter_web
When I run 'pub dev', an error occurs while resolving the package stopper. Then I tried to open github from the package stopper (https://github.com/aryzhov/flutter-stopper), after that I checked the files in the lib folder. There I found a file called stopper.dart. It turns out that in the file, still using
 import 'package: flutter / material.dart';
So that makes my pubspec error. Because the code should be replaced with
import 'package: flutter_web / material.dart';
Therefore, I tried to outsmart the problem by removing the dependency stopper on pubspec.yaml, then I created a stopper.dart file manually, then I saved it in the lib/mypackages folder. After that, I import stopper.dart from the lib/mypackages folder (it's no longer through the package).
The package stopper is just one of the packages that I use on my Android project. Actually there are many other packages that have the same problem as the package stopper. The point is I have to create the package files manually, then I change "package: flutter / ..." to "package: flutter_web /" manually, then I can import them.
What I want to ask, is there a more elegant and simple way?
Hi the instruction you are following is from an old repository here. As you can see this is discontinued. You can find the informaiton in the REadme.
Instead you should follow the instructions in this page. Read it once carefully and I would suggest to keep a backup of your main project and work on a copy.
First you will have to enable the flutter-web using flutter config --enable-web.
Then you will have to run flutter create .

How can I run an example from a Flutter package/library?

I have the following simple directory structure:
flutter_published
.idea
android
build
ios
lib
main.dart
flutter_published.iml
pubspec.lock
pubspec.yaml
network_to_file_image
.idea
example
main.dart
lib
network_to_file_image.dart
test
network_to_file_image.iml
pubspec.lock
pubspec.yaml
network_to_file_image is a package.
There are two main.dart files, one at flutter_published/lib/main.dart and
another at flutter_published/network_to_file_image/example/main.dart
I am able to run the first one, but not the one inside of the example directory under network_to_file_image. The second one gives me this error:
Launching example\lib\main.dart on Android SDK built for x86 in debug mode...
No application found for TargetPlatform.android_x86.
Is your project missing an android\AndroidManifest.xml?
Consider running "flutter create ." to create one.
Also, when the app is generated, what happens to the example and test directories of the packages I use? Are they included or removed from the final app that is deployed?
To solve this, instead of the main.dart file inside of the example directory, you need to create a complete Flutter application-type project inside of the example directory. Then, the example tab will point to the README.md file inside of that directory.
That example directory will have its own lib directory, containing a main.dart file. Since that file is now inside of an application-type directory it can be run.
Visit this repo to see how it works:
https://github.com/marcglasberg/async_redux/tree/master/example
Update:
To be clear, the example's pubspec.yaml file can reference its package by using a relative reference. For example, here is the dependencies section of the example dir of the async_redux package I mentioned:
dependencies:
http: ^0.13.1
async_redux:
path: ../
flutter:
sdk: flutter
Since the example dir is at the same level as the package's pubspec.yaml file, then the example's own pubspec.yaml is one level below it. Thus, it may reference the package itself by using a ../ path:
async_redux:
path: ../
example/main.dart only exists to be shown in https://pub.dartlang.org/packages/network_to_file_image#-example-tab-
The pub site is limited in how it finds content in the example directory to display in the Example tab.
cd to the directory and execute flutter create .. You should be able to run it afterwards
Go to the example folder of the repository of the package.
Open the lib folder and go to the main.dart file.
Above the void main() function, you can see the Run|Debug|Profile (pic below), click on the one you want to run the project as.
The example app will now run in your emulator.
The screenshot below is of the chewie package available on pub.dev