How to develop Flutter app and related package at same time in VS Code - flutter

I have a Flutter app and a package folder loaded in VS code at the same time within a workspace. What entry do I need to make to my app's pubspec.yaml file to ensure that changes I've made to the package are compiled and included whenever I hot reload or restart the app? What would be an alternate strategy if this is not possible?

If your pubspec.yaml refers to your package with a path then I would expect this to happen automatically. If not, I would consider it a bug. Please file an issue at https://github.com/Dart-Code/Dart-Code and include a log file generated by running the Dart: Capture Logs command and as much info about your project layout as possible (a clonable repo to repro would be perfect).

Related

Flutter optional Assets

I'm building an app in Flutter, and I'm using the flutter-dotenv package so we can change things around on development time or testing time without changing the code.
I can see that for it to work I must include a .env asset in the pubspec YAML file, however, that .env file may or may not be there. Currently I'm just using an empty .env file, but I don't think this is ideal.
Is there any way to instruct the flutter build process to copy a file only if it exists?
Thanks!!

Unverified breakpoint in VSCode in Flutter

My debug mode does not work as I see the unverified breakpoint notice in VSCode in debug mode.
I already checked the Launch json file and it is fine, but something else is wrong which I do not see what it is. Maybe something related to paths, etc.
I tried to make a new project in VSCode using the command and the new default Flutter project is OK.
So it seems like, if I can make a fresh project from my current project then my problem could be solved. But how? I tried to move my files from my current project to the newly made Default Flutter project but I do not think this is the way. Because I guess some of the files in my project are the source of issues...
I already deleted .vs file but no help.
Do you have any good idea how to fix this issue?
First try cleaning the project files using flutter clean then flutter pub get, try running the app without debugging and if you are down for making a new one just move the lib and assets folders and copy the content of pubspec.yaml file.

How to create/provide multiple example apps for a flutter plugin?

Flutter seems to have a very strict/rigid structure for plugins with example folder and all contents inside that folder.
I want to provide multiple examples with my plugin. Something like examples folder and then examples/demo1 and examples/demo2 as two different app examples.
I tried doing this but flutter run or pub get command breaks with this change. it's gets stuck with below error which wasn't thrown with exact same code in previous structure before change. Also my app actually follow embedding v2 code so this error is completely false too.
The plugin `<MY PLUGIN>` requires your app to be migrated to the Android embedding v2. Follow the steps on
https://flutter.dev/go/android-project-migration and re-run this command.
Somehow is it expecting that there should be only one example and that too with example folder only ?
Can someone help, and if possible point me to a plugin project where it's using multiple examples ?
After trying multiple ways to deal with the situation,
I ended up with a good enough solution.
I moved entire flutter repo inside an sdk directory, and then Introduced a samples folder at root level which can contain multiple sample applications.
sdk itself has a default sample app under example folder which I kept so sdk can ship with one example project.
Final structure look like below,
- Root
- sdk
- Flutter plugin project (like android/example/ios/lib directory etc.)
- samples
- sampleOne
- sampleTwo
I then mentioned relative path to sdk for sampleOne or sampleTwo inside their pubspec.yaml like path: ../../sdk/
When I want to open sampleOne in AndroidStudio, I import sampleOne directory and it works like charm. Make sure you don't import entire samples directory or sampleOne/Android.
For any regular Flutter commands for plugin, I run them inside sdk directory and everything works fine as expected. So I would run publish or pub get inside root/sdk directory

Flutter Package URI doesn't exist inside package's test

Inside the Flutter project directory, I created a package with the following command :
flutter create --template=package emoji_support
After it completes creating the package and completes flutter get.
But..
Files inisde package's /test directory can't find files inside package's /lib directory :
import 'package:flutter_test/flutter_test.dart'; is resolved ✅
But...
import 'package:emoji_support/emoji_support.dart'; is not resolved ❌
File structure
What error am I making here?
I created a package inside my project flutter_example_file_picker with flutter create --template=package emoji_support
Then in the pubspec.yaml of the project (not the one inside the plugin) flutter_example_file_picker > pubspec.yaml I added to the dependenceis the package
dependencies:
flutter:
sdk: flutter
emoji_support:
path: ./emoji_support
and the problem resolved, I could run the test with no problem, also tried with a relative path without adding it to the dependencies and worked too
UPDATE
I think I undestand how to fix it without adding it to the pubspec, when creating a plugin inside a project, the flutter plugin of AndroidStudio (or VS) is still working in the main route (check the terminal dir and it will be C:/.../Workspace_Android\hundreddaysofflutter> so it doesnt detect the inner plugin) and doesn't update to detect the new folder. Even if it's red and says it cannot detect the URI.
You can ignore it and try to run the test and see if it detects the inner dart_tool with package_config.json, then it will run succesfully and the problem will dissapear. The second option is to move to the folder where the plugin is (in the terminal cd my_plugin_name_folder or just file open and open the plugin).
Run flutter get pub (it does it automatically when creting the first time a project but sometimes when you create one inside another it doesnt do it) to run for the first time the package and create the package_config.json inside dart_tool (the dart_tool of the plugin), at the end of the file you can see the name and rootUri of the package, now you can use it in your test nad it should detect it correctly. Sometimes it's generated but it seems it doesn't update correctly and the IDE doesn't know even if it's there.
This is just a problem that occurs , when you are adding new sub_directories in vsCode.
just a simple exist and re-open will fix the issue.
tell me if that fix it.

Where does `pub get` download pubspec dependencies to?

In javascript, we have NPM and the node_modules folder in every project. I was not able to find a similar concept for Dart/ Flutter, except the build folder in my app, which contains a folder of a few dependencies I have in pubspec.yaml. It doesn't have any of the source code though, and I think it's actually built from something else. I've also looked in /usr/local/flutter/packages which is where my flutter is installed, but it only shows flutter_driver, flutter_goldens, and more seemingly unrelated folders.
I guess if wanted to read the source code, I really need to find the repo and read from that, or is there a location for dependencies I haven't looked?
I even found projectDir/.dart_tool/pub, which didn't have any of my packages.
From the documentation:
Dependencies downloaded over the internet, such as those from Git and
the pub.dev site, are stored in a system-wide cache. This means that
if multiple packages use the same version of the same dependency, it
only needs to be downloaded and stored locally once.
By default, the system package cache is located in the .pub-cache
subdirectory of your home directory (on Mac and Linux), or in
%APPDATA%\Pub\Cache (on Windows; the location might vary depending on
the Windows version). You can configure the location of the cache by
setting the PUB_CACHE environment variable before running pub.
So for Mac and Linux for example, this would be ~/.pub-cache/hosted/pub.dartlang.org by default.
In Android Studio
I could directly view the source code of the package under `
[External Libraries/Dart Packages/Your Packages]
`
You can get pubspec downloaded from your flutter sdk location .
/flutter/.pub-cache/hosted/pub.dartlang.org/
You can also clone package git .
If you are running Windows as your OS, you can find the packages under the folder that you installed your Flutter SDK to when setting up Android Studio.
In my case - using Windows 10 - the path is as follows, where C:\ is my primary harddrive and flutter\ the folder containing the Flutter SDK...
C:\flutter\.pub-cache\hosted\pub.dartlang.org\english_words-4.0.0
The above path for instance points to the "english words package", containing the most ~5000 used English words and some utility functions, which are mentioned and used in the Flutter tutorial on their official page for writing and running your first Flutter app.
https://pub.dev/packages/english_words
If you have installed flutter using snap then the location might be
/home/user/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org
I had forgotten that I've been command + clicking into these libraries all the time in VSCode.
However, it looks like packages are not stored in the app folder. Packages that we use in our projects are downloaded to $FLUTTER_PATH/.pub-cache, so if I'm looking for camera picker plugin, its in
/usr/local/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.5+2/lib/image_picker.dart
If you are using VScode you can find these dependencies under the dependencies window: