Is there a way to apply Flutter build_runner to dependencies? - flutter

I have a Flutter app project app and a dependency project lib that contains generated code by
json_serializable, built_value, etc..
app builds successfully when lib has generated code like source.g.dart.
But when source.g.dart does not exists, app build failed and I have to rerun build_runner on lib.
And I'm going to host lib on GitLab and don't want to track *.g.dart on git.
So, every time I clone or pull code that needs code generation from GitLab, I must remember to run build_runner on lib or get error:source.g.dart does not exists.
Off course I can write script like below.
run build_runner on lib
build app
But I believe there is a better way to tell the build system from lib,
Hey, I want to run build_runner on me.
Is this possible ?
If so, please let me know.

Related

Flutter command works locally but not on Github Actions

I have a problem where a command that works on my local machine isn't working on GitHub Actions.
The command I'm using on my local machine is:
flutter build appbundle --obfuscate --split-debug-info=build/app/outputs/ --flavor Production --dart-define="ENVIRONMENT=PRODUCTION"
This command exists in the yml. If I change it to flutter build apk then it works with no problem.
However, when trying to build the aab I get the error:
Gradle build failed to produce an .aab file. It's likely that this file was generated under /home/runner/work/{project-name}/{project-name}/build, but the tool couldn't find it.
I'm not sure why it's put the project name in there twice, and I don't know if it's the cause or it's what it's supposed to be.
When I looked up the error I found people saying it's because I didn't specify the flavour. Clearly I have specified the flavour, so I'm confused as to what's happening.

Project files can't see library

There are many libraries I use every day from pub and they work. But today I run into a problem with qr_code_scanner. After installing it with flutter pub add qr_code_scanner and running flutter pub get, I get in code:
Same with auto import for classes from this file like QRViewController. What I also did is running flutter upgrade, flutter clean and everything else that came to my mind - still getting this error with that package.
Any other thoughts how can I solve it?
Some of the packages you are using are upgraded to flutter3 and some are still Flutter version < 3. Which is why you might be facing an issue. If you are trying to build an older project I would recommend you to downgrade your package versions which is compatible with flutter < 3 and use flutter version < 3. If you are adding any packages through pub add you might get an error if the pub get is not resolved. Please check the same in terminal.
For anyone who will have the same issue, I found a workaround. I've also tried few other libraries that beside the fact was installed couldn't access them from anywhere in code.
So after all every pub.dev lib is just the code right? You can download the source file and add it into your lib folder. Well, you have to upgrade it manually if needed but at the end it works and that's the main goal!
EDIT:
Another possible solution, as I found out, I did reset my Mac, upgraded environment (VS Code), done flutter clean and flutter pub get and looks like it's working as it should without mentioned workaround so probably the issue was around the coding environment rather than with Flutter and fetched libraries.

app-release.apk not installing after changing package name in flutter?

I have changed the package name and name of my application. After changing it, the build apk_file was not installing in device. Can any one have solution for this.
Note: I am using VS-Code.
PFA
I have added --no-shrink in command of building apk.
flutter build apk --no-shrink
Hope it will work.
Thank you.
That could be because of the debug and profile manifest files as well as the kotlin file, the package name needs to be changed there too.

How to generate flutter code before every build

I'd like to have the output of some git describe call available inside my flutter app. It's important that this is run automatically before builds triggered by i.e. flutter run or flutter build apk. If I have to remember running a command manually before rebuilding the app in my IDE I won't get an accurate version number in the app.
I looked at https://pub.dev/packages/build_runner but that seems to require to always invoke it manually.
I also looked at --dart-define but through this I can just pass values into dart code but I also need a way to automatically update that value.
I did get this to work through gradle... but that is only invoked for android builds. Adding it additionally to the cmake layer would work... that still leaves web and iOS builds without a solution.
Anyone got an idea what I might be missing?
(flutter can already generate code for translations dynamically via the generate: true flag but this doesn't seem to be exposed through any way to add custom generation commands there?)

Update flutter dependencies in /.pub-cache

I erased in my folder .pub-cache/hosted/pub.dartlang.org/this_plugin
What is the command to update the dependencies inside pubsec.yaml? I believe it is
flutter packages get
The folder under .pub-cache is still not up to date.
Note: there was a pubspec.lock that I deleted
Disclaimer: By running the command below, have a really fast internet
connection or be ready to lose one hour of productive hours. ( it will
redownload every package every installed on your pc, and I mean each
and all of the versions of each packages)~TSR
flutter pub cache repair
or delete /Users/xxxxxxx/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/ and run flutter packages get again.
if all the above things fail delete the cache folder or also check the version updated in the lock file (some time lock give me the problem with updating the version number)
PS:
Why flutter pub cache repair download every package version previously used?
Remove the package from dependencies in pubspec.yaml, run flutter packages get. And then add the package to dependencies again and running flutter packages get.This process has solved the problem for me in the past.
This steps worked for me
Delete the pubspec.lock file then run the command flutter pub get again.
Clearing the projects cache
You can empty the entire projects cache to reclaim extra disk space or remove problematic packages.
run flutter pub cache clean in terminal at project directory
The below steps worked for me.
Flutter installation corrupted
The Flutter install directory is in an inconsistent state and that causes all kinds of troubles.
Causes
Unclear
Ways to fix
Run the following commands in the Flutter install directory:
git clean -xfd
git stash save --keep-index
git stash drop
git pull
flutter doctor
Corrupted cache
Especially with plugin packages, it was seen several times that the package in the pub cache was corrupted.
Symptoms
Usually syntax errors at build time about code in dependencies.
Causes
Unknown.
IDEs or editors used by developers might not prevent editing plugin files and when they navigate into plugin code they might accidentally modify the code.
Ways to fix
Run flutter pub cache repair
This might take quite some time and re-downloads every package in the cache, even outdated versions that might not be used anymore by any project on disk.
Delete ~/.pub-cache/hosted and/or ~/.pub-cache/git (for Git dependencies). This requires running flutter packages to get in all active projects on your machine afterward.
Delete a specific package or package version. Look up the exact path in the cache for a specific package in the .packages file of your project. For example for firebase_auth
Reference: https://github.com/flutter/flutter/wiki/Workarounds-for-common-issues#flutter-installation-corrupted