flutter function

Future<ProcessCompletion> flutter(
  1. List<String> args, {
  2. Directory? workingDirectory,
  3. Progress? progress,
  4. bool nothrow = false,
  5. String throwOnError()?,
})

Executes Flutter command from Flutter SDK set in flutterSdk

Set nothrow to true to ignore errors when executing the flutter command. The exit code will still be non-zero if the command failed and the method will still throw if the Flutter SDK was not set in initializeSidekick

If throwOnError is given and the command returns a non-zero exit code, the result of throwOnError will be thrown regardless of nothrow

Implementation

Future<ProcessCompletion> flutter(
  List<String> args, {
  Directory? workingDirectory,
  dcli.Progress? progress,
  bool nothrow = false,
  String Function()? throwOnError,
}) async {
  final sdk = flutterSdk;
  if (sdk == null) {
    throw FlutterSdkNotSetException();
  }

  await initializeSdkForPackage(workingDirectory);

  int exitCode = -1;
  try {
    final process = dcli.startFromArgs(
      Platform.isWindows ? 'bash' : sdk.file('bin/flutter').path,
      [if (Platform.isWindows) sdk.file('bin/flutter.exe').path, ...args],
      workingDirectory: workingDirectory?.absolute.path,
      nothrow: nothrow || throwOnError != null,
      progress: progress,
      terminal: progress == null,
    );

    exitCode = process.exitCode ?? -1;
  } catch (e) {
    if (e is dcli.RunException) {
      exitCode = e.exitCode ?? 1;
    }
    if (throwOnError == null) {
      rethrow;
    }
  }
  if (exitCode != 0 && throwOnError != null) {
    throw throwOnError();
  }

  return ProcessCompletion(exitCode: exitCode);
}