Flutter optional Assets - flutter

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!!

Related

where does the variables defined using "--dart-define" go and can we reverse engineer them?

I am building a flluter application.
I don't want to compromise my secret_key by putting it in the code, so I tried making a .env file and created an apk. Then I unzipped the apk and found my config file there. So now I am not doing that.
The next thing I tried is using --dart-define variable declarations to put my secret_key while building the app and I am accessing it using
const secret = String.fromEnvironment("secret_key");
Coming to the question, where do these variables go inside the dart code and is there a way to get them by reverse engineering.
Basically is it safe to put my secret key this way?
From what I found, you can find the --dart-define variables in the binary file generated for each ABI, so yes, you can reverse-engineer it.
How to try:
Call the variable from your code with String.fromEnvironment("ANIMAL").
Run flutter build apk --dart-define=ANIMAL=Dog to build for Android
Open the generated .APK file with a file archiver (7-Zip, for example) and navigate to /lib/(ABI)/
Open /lib/(ABI)/libapp.so file with a text editor or hex viewer and search for the value Dog and you will find it
Observations:
If you don't use the variable in your code, it won't be added to the binary
Using --obfuscate with flutter build won't help, because it doesn't obfuscate environment variables

How can I use environment variables in flutter without adding a file in pubspec.yaml

At the moment I am using flutter_dotenv to load some environment variables from a file. It worked okay until I tried to create a GitHub Action that runs flutter test.
The file with the environment variables has to be added as an asset in pubspec.yaml acording to the documentation for the package so when I run the action I get the following error:
No file or variants found for asset: dotenv.
Where dotenv is the name of the file with environment variables. The reason I get the error is because dotenv is in .gitignore (obviously), so that file can't be found.
Is there a way to use environment variables in Flutter without having to add the file to pubspec.yaml? Or is it possible to add the file "locally" (meaning adding it to GitHub without having to track it with git) for the action in some way?

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

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

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

Co-locate flutter test files to source

Putting test files in the test directory involves mental gymnastics of remembering to move test file when sources move or to even find a corresponding test file.
Is there a way in flutter to place test files next the source?
flutter test -h doesn't have any path related options.
This can be achieved with
flutter test lib/**/*_test.dart
Found the hint from Dart test package readme at https://github.com/dart-lang/test/tree/master/pkgs/test#running-tests