How to execute binary file from assets in dart flutter?

Viewed 32

I have an app that executes a custom binary file compiled from another language for some reason. I want to execute it using Process.run from dart but I was getting permission denied.

class _MyHomePageState extends State<MyHomePage> {
  ...

  void _executeBinary() async { 
    final byteData = await rootBundle.load('assets/my_executable');
    final buffer = byteData.buffer;
    Directory tempDir = await getApplicationDocumentsDirectory();
    String tempPath = tempDir.path;

    File file = await File('$tempPath/my_executable').writeAsBytes(
      buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)
    );
    
    // Permission denied :(
    ProcessResult rs = await Process.run(file.path, []);

    if (rs.stdout.isNotEmpty) {
      print("Log: ${rs.stdout}");
    }

    if (rs.stderr.isNotEmpty) {
      print("Log: ${rs.stderr}");
    }
  }

  ...
}

This is the actual error message from flutter:

E/flutter ( 3343): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ProcessException: Permission denied
E/flutter ( 3343):   Command: /data/user/0/com.example.webrtc_test/app_flutter/my_executable

How do I fix this problem?

0 Answers
Related