How to generate flutter code before every build - flutter

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?)

Related

How do I run a command line tool that depends on Flutter?

The flutter_test package is able to run Flutter apps 'headless', without requiring any embedder. It would be useful to me to be able to run a command line tool (i.e. not a test) in the same mode. Is this possible? I looked at the flutter_test package but nothing there seems to be the magic solution.
What I've tried:
Running a CLI tool that depends on Flutter with dart path/to/tool.dart. This throws Error: Not found: 'dart:ui'.
Running it with flutter run path/to/tool.dart. This will attempt to start the CLI tool as a regular Flutter app on a device (such as an Emulator, the desktop OS, or a web browser).
Running with flutter pub run path/to/tool.dart just generates a lot of errors, such as "Error: 'Paint' isn't a type". I tried all approaches suggested in this related SO Question, with the script living in various directories and using different invocations, but no luck.
Running the tool using flutter test path/to/tool.dart does work, and it runs the code in main() like one would expect. But I consider this a hacky workaround, since flutter test (unsurprisingly) expects a test, and exits with the error "No tests were found." And, semantically, it's just wrong. This is not a test. It's a tool.
If your code relies on package:flutter or dart:ui, you won't be able to run it outside of a Flutter engine instance (e.g., flutter_tester or a Flutter application) as dart:ui is a custom core library for Flutter that isn't part of Dart's core libraries. Short of hacking away at flutter_tester to remove the test runner related logic or creating your own custom embedder of the Flutter engine, you won't be able to run a Flutter project headless on the command line.
Ideally, you'd just refactor your tool to not depend on dart:ui at all, but given your background I'm guessing you're trying to do something non-standard that actually requires functionality from the Flutter framework... :-)

Flutter Windows Desktop app gets stuck on white screen after msix installation

Background
I just developed my first flutter desktop app for a windows machine. The app is working fine while developing/debugging it, but I am trying to test it as an application in release mode.
What’s done
I created an msix using pub msix.
For signing, I have tried both my
own certificate and the test certificate that comes with msix
packages by default.
I have tried both stable and beta channels.
Actual Problem
When I install the app on the other system (or even on the actual system where I debugged the app). I can install the msix setup successfully I can see my app listed in the apps, but when I try to open it, it is stuck on a blank/white screen. There is no UI rendered. And if I try to maximize the window, it goes to App not responding state.
Any help will be really appreciated.
Thanks in advance.
Update / New Finding
After going through the links provided by Yehuda Kremer and pulling my hair for a few days, I found the answer in this tweet
https://twitter.com/FilledStacks/status/1436280577439715338?s=20
So the main issue is that app is using some absolute paths that don't work on other machines (after release)
1- My app was using a database, so I have made sure that that database path is relative to the application document directory.
2- Now the build is also referring to some dependencies and their paths are also absolute. I have noticed a flag CMAKE_INSTALL_LOCAL_ONLY in the cmake_install.cmake but that flag is not being set anywhere.
Here is the log of flutter build windows -v
https://pastebin.com/LAeshUMY
-- Now I am looking for a proper way to convert all the paths to relative so that the build is ready for installation on different machines, instead of local installation only.
I have found the solution.
The main problem was that the app was looking for some dependencies and it was unable to find them. As mentioned here.
The reason for not finding that in release mode can be one of the following,
You are using some dll (package that depends on a dll) that is available on a specific path in your system, but when you are release the app that (absolute) path is not valid anymore.
You are adding some asset with absolute path (that is only applicable to your system).
Solution:
If it is a dll, you should be adding that to the release folder. In my case it was sqlite3.dll
If it is some other asset then you should always use the relative path.
You mentioned msix package, let see if the problem is in the app or in the packaging process
run flutter build windows without the flutter pub run msix:create, then run the created .exe file to see if its work.
btw this issue mention here: https://github.com/flutter/flutter/issues/74507
and here (the 'maximizing' problem): Black screen appears on maximizing and minimizing flutter desktop app

I could link the package with my project using 'npm-link'. But, Some how in the browser, the changes are not reflecting properly

I have a component library [design-system]. I want to use that library in another project.
I did run npm-link in 'design-system' and npm link design-system in the project.
First, the link didn't work, after I deleted package-lock.json and did npm i. I could see, my component refers to the design system's dist index path. Even If I change anything in the design system and build it. It is reflecting immediately in the project. I can see the design-system's code in VS Code IDE.
But, when we run the program, the changes are not reflecting in the UI.
For example,
I commented on the console.log code in design-system
even I can see this code in the package by clicking 'Go to Definition'. But in the browser, the code is not updated.
I cleared 'Application cache' and tried again. I don't know what I am missing. Please help me with this.
Thanks!
you need to run the build in watch mode for your design-system library. Now for each change, the library will build and the bundle will update hence you will see the changes reflecting.
Alternatively, for development purposes, you can do something like
"design-system": "file:../relative-path-from-pkg-json"
instead of npm-link. still, if you can target the non-build version of your library. it may or may-not work deponding on your build tooling.
Or you can get real fancy and try Nx Monorepo approach which will take care of everything about tooling. while you can focus on your development.

Why `Flutter Doctor` and `New Application Project` fails in my VSCode setup?

Following these directions, like:
Install the Flutter and Dart plugins
Start VS Code.
Invoke View > Command Palette….
Type “install”, and select Extensions: Install Extensions.
Type “flutter” in the extensions search field, select Flutter in the list, and click Install. This also installs the required Dart plugin.
Validate your setup with the Flutter Doctor
Invoke View > Command Palette….
Type “doctor”, and select the Flutter: Run Flutter Doctor.
Review the output in the OUTPUT pane for any issues. Make sure to select Flutter from the dropdown in the different Output Options.
Step 3 of validate instructions results with OUTPUT:
I did install flutter, and flutter doctor in a new command shell gives result:
What am I missing here? I also try VSCode extension palette command Flutter: New Application Project and that also fails with:
Make sure that you have installed Git and that you can access it from the command line. Check your PATH. Flutter SDK directory should be present there.
If that does not work, you should try to reinstall the dart and flutter extension.
Next Step should be to reinstall flutter.
It occurred to me that my attempt to support multiple flutter versions might be going outside of the conventions supported by the Flutter extension for VSCode.
Here's what's up!... If you think you're smart like me and installed different versions of flutter in folders like c:\src\flutter\1.20.2 and c:\src\flutter\1.22.6, then you can forget it because the extension is smarter than us, and will ignore this regardless of our PATH assignment.
Copying contents of my flutter install from c:\src\flutter\1.22.6 to the parent folder c:\src\flutter resolved my issue. I need to review how to easily support different versions of flutter. Hmmm..
BTW... what I realized in retrospect is that the complaint about git is raised by the extension not finding the .git folder under the flutter folder (i.e. c:\src\flutter) that it presumed as the path to my flutter install. Well duh!! That's because I want flutter to be sourced at the versioned sub-folder (i.e. c:\src\flutter\1.22.6). Oh well.
Some scenarios I am left pondering:
How to correctly support multiple flutter installations while also benefiting from the extension (is there a way to configure this extension or my environment as a whole)?
Are my expectations to support multiple flutter installations unreasonable?
If my expectations are reasonable, and there are no known or documented way to support multiple flutter installations while also using this extension, then perhaps the extension has a bug?

Flutter web throws error on same project but different laptop

So I just won a new laptop in a competition and now I am having some trouble migrating my flutter projects. I have set up the editor and migrated the important files in the code, i.e the lib, image, font, pubspec.yaml and web. But this app is now throwing the following error
Launching lib\main.dart on Chrome in debug mode...
Waiting for connection from debug service on Chrome...
ERROR - 2020-12-29 13:08:07.845202
GET /main_module.bootstrap.js
Error thrown by handler.
AbsoluteImportUriError: 'file:///C:/FlutterProjects/xspectre/lib/help/Auth.dart'
package:dwds/src/debugging/metadata/provider.dart 154:9 MetadataProvider._addMetadata
package:dwds/src/debugging/metadata/provider.dart 133:15 MetadataProvider._initialize.<fn>
I thought it may be a problem with my flutter but it runs the default flutter app without problem.
I also tried deleting the file but the problem just shifts to another file. I can safely say that this a is a problem with the help folder which provides me essential tools such as the auth service. What should I do? I will provide any code neccesary. You can find the entire code here
The Error:
The error is happening because when importing your new project, some imports in your .dart files are now using absolute paths instead of relative paths. In your case, this is happening at least in one .dart file that is calling your Auth.dart.
Sloution:
Step 1: Look at the top of all your .dart files, more precisely at every single import. You will have to change everything starting with:
import 'file:///C:/...
by
import 'package:... and add the name of your app
Step 2: Rerun your project and everything should work now
If still issues, then make sure you looked at all your files to ensure you did not miss other wrong import... also, you can run flutter clean then run a Pub Get
(thanks to melwinlobo18)
Please mark the answer if this was useful ;0)