Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass arguments into my application's main() #170

Open
GoRadioGo opened this issue Apr 9, 2021 · 4 comments
Open

Pass arguments into my application's main() #170

GoRadioGo opened this issue Apr 9, 2021 · 4 comments

Comments

@GoRadioGo
Copy link

Is there a way to get arguments into my main(), similar to below ?
void main(List<String> arguments) async { print('args: $arguments'); // ... do my stuff ... }

I tried adding a "custom" command line option in the [flutter engine options] portion of the flutter-pi command line, but arguments was still an empty List.

@pezi
Copy link

pezi commented Apr 10, 2021

Yes you can, but it is a little tricky:

flutter-pi asset_dir param1 param2 
import 'dart:ffi';
import 'dart:io';

bool _isFutterPi = Platform.resolvedExecutable.endsWith('flutter-pi');

bool isFutterPiEnv() {
  return _isFutterPi;
}

var _flutterPiArgs = <String>[];

List<String> getFlutterPiArgs() {
  if (!isFutterPiEnv()) {
    return const <String>[];
  }
  if (_flutterPiArgs.isEmpty) {
    final dylib = DynamicLibrary.open('libc.so.6');
    var getpid =
        dylib.lookup<NativeFunction<_getpId>>('getpid').asFunction<_GetpId>();
    var cmd = File('/proc/${getpid()}/cmdline').readAsBytesSync();
    var index = 0;
    for (var i = 0; i < cmd.length; ++i) {
      if (cmd[i] == 0) {
        _flutterPiArgs
            .add(String.fromCharCodes(Uint8List.sublistView(cmd, index, i)));
        index = i + 1;
      }
    }
  }
  return List.unmodifiable(_flutterPiArgs);
}

Should return following list

  • flutter-pi
  • asset_dir
  • param1
  • param2

@ardera
Copy link
Owner

ardera commented Apr 10, 2021

I tried adding a "custom" command line option in the [flutter engine options] portion of the flutter-pi command line, but arguments was still an empty List.

Yeah the engine options are passed to the engine as-is, those are not the cmdline arguments for the main function of the Dart VM. I think @pezi's solution is a good way to do it. You could also use environment variables and fetch those using Platform.environment. I have no idea if its possible to pass arguments to main itself.

@ardera
Copy link
Owner

ardera commented Apr 14, 2021

Seems like flutter supports passing arguments to the app's main since 2.0. I'll add a way to pass arguments. Though not sure where to accept those arguments in the flutter-pi invocation.

@GoRadioGo
Copy link
Author

Great, I will follow that. I am still on 1.x, but hope to migrate to 2.x engine binaries next month.
If not feasible, definitely a workaround along the lines of @pezi is always possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants