How to bundle native macos executable with a flutter app? - flutter

I want to bundle a native console program for macos written in Rust, into a Flutter app.
The Flutter app needs to call this program from Dart with something like this:
Process.runSync("./myconsoleapp", ["argument1", "argument2"]);
The Flutter app can't seem to find myconsoleapp.
Is there a way to embed myconsoleapp into the Flutter app, and make it executable? I tried manually adding and it didn't work.
Steve

Flutter for windows simply bundle project.exe along with a bunch of other files into a directory in (build\windows\runner\debug\). No one can stops you from adding your files to that directory.
In your dart code, simply start a new process from the newly added file:
var exePath =
p.join(p.dirname(Platform.resolvedExecutable), 'myFile.exe');
var result = await Process.run(exePath);

It can't find your executable because there is no relationship between ./--which is the current directory in the environment your app was launched from--and your application's location. If you want to access something bundled in your application you should be using NSBundle's pathForResource:ofType: to get the path to it (either via a method channel, or FFI).
Is there a way to embed myconsoleapp into the Flutter app,
You would add it as a bundled resource via Xcode. (There's nothing Flutter-specific about this step.)
and make it executable?
Presumably the product of your Rust build process is already executable.

Related

Flutter Hive - how to locate box files

This may be obvious, but can someone please tell me how I can locate my Hive Box file after initialising Hive in a Flutter application, using Android Studio?
I have followed the documentation here - https://docs.hivedb.dev/#/README - and my basic implementation works o.k, but I cannot find where the Box file, or any Hive files for that matter, are stored.
I want to be able to locate where my Box (database) is within my Project in Android Studio, so that I can see the contents of the database and confirm it exists in the right place
This is my code where I'm initialising Hive and opening a box
void main() async {
// Initialise Hive
await Hive.initFlutter('hiveusersfolder');
runApp(MyApp());
var box = await Hive.openBox('hiveusersfolder');
box.put('name', 'MyName');
print('Name: ${box.get('MyName')}');
}
The above code is present in my main.dart file
I am using Android Studio on Ubuntu.
*Edit update
I have located the supposed path of where the Box is stored, but when using Android Studio Navigate > Search Everywhere - there is nothing found within this folder/path. After initialising my database and adding a user to the box, I should be able to see a box file and view the users like with any other kind of db no?
This is the path /data/user/0/myappv1.myappv1/app_flutter
I tried Invalidate Cache + Restart, but still nothing
If the problem is with Android Studio search, how can I access the above path on Ubuntu file explorer? Or using the command line?
It is clearly stated in the documentation
/// Initializes Hive with the path from [getApplicationDocumentsDirectory].
///
/// You can provide a [subDir] where the boxes should be stored.
Future<void> initFlutter([String? subDir]) async {
Solved this by selecting a Project SDK from File > Project Structure
I then had to make sure I was using an Android Virtual Device with a system image that did not support Google Play store (Pixel 2XL with Android 8 I think, just picked anything without Play).
There is an issue where if you're using an AVD with Google Play loaded you won't be able to access the default folder where Hive puts your .hive files
Your Hive files by default, at least on Android with Flutter, seem to go into this path - /data/user/0/yourapplicationv1.yourapplicationv1/app_flutter
Hopefully this helps anyone experiencing the same issue
In the case of iOS, you can check it by following the steps below.
First, check the identifier of the simulator you are using in the application you are developing, which can be found in xcode under Window → Devices and Simulators. The identifier is usually formatted something like B8D6964E-01BD-4D90-96CA-746F87AF3F48.
 2. run $ cd ~/Library/Developer/CoreSimulator
 3. run $ cd YOUR-SIMULATOR-IDENTIFIER
 4. run $ find . -name "*.hive" -ls
This will probably find the .hive file. However, in order to see the contents of this file, you will need to install Hadoop and Hive.
I haven't tried it because it seems a bit time-consuming, but I'll post a link that might be useful.
how-to-install-hive-on-mac-with-homebrew
In my case, I am implementing a macos application, the hive folder was:
/Users/me/Library/Containers/com.developerId.applicationId/Data/Documents/
Put a breakpoint on the await Hive.initFlutter('hiveusersfolder');, and step into it. Keep stepping in until you get to the getApplicationDocumentsDirectory(), and you will see what path that function returns.

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 get .exe file from my flutter desktop application?

I have built a desktop application with flutter. Now wanna share its .exe to another person.
I have browsed for it and got some answers but according to this, I couldn't found.exe file.
First I run
flutter build windows
to release .exe file.
I went through the path but it created some strange file
build\windows\x64\Debug\
and found these 3 folder:
The actual .exe is under build\windows\runner\Release. Good luck!

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

How to run phone gap with xcode4?

Since moving to XCode4, I have been getting errors like:
/VERSION: No such file or directory
cp: /javascripts/phonegap..js: No such file or directory
cp: /javascripts/phonegap..min.js: No such file or directory
error: /VERSION: No such file or directory
for projects that were working under XCode3.
Open XCodes Preferences, and navigate to Source Trees. If there is no PhoneGapLib entry there, try adding a new setting with the following values:
Setting Name: PHONEGAPLIB
Display Name: Phone Gap Lib
Path: /Users//Documents/PhoneGapLib
Note that the path should be to the location of your PhoneGapLib folder, and that it may not be in your documents folder, depending on how you installed PhoneGap.
I just learned about a great web service recently made available by Nitobi (makes of PG), which will automatically generate the necessary PhoneGap files you need for use in Xcode 4.
Just enter your project name, hit a button and they'll generate a zip file for you to download. This lets you set up a new project without messing with the command line.
You could set up a new project, then migrate your older project files over.
Did you search Google for this error?
I'm a bit of a noob with PhoneGap and Xcode still, but I know there have been recent issues with PG and Xcode 4. Perhaps you're experiencing the same issues as the commenters to this post: PhoneGap + XCode4 (and more specifically, here).