createProject method

Future<String> createProject({
  1. required String projectName,
  2. String? workingDirectory,
  3. String executable = 'flutter',
})

Creates a new Flutter baseline project named projectName in workingDirectory.

Returns the absolute path of the created project directory.

Implementation

Future<String> createProject({
  required String projectName,
  String? workingDirectory,
  String executable = 'flutter',
}) async {
  final ProcessResult result;
  try {
    result = await _runner.run(
      executable,
      ['create', projectName],
      workingDirectory: workingDirectory,
    );
  } on ProcessException catch (e) {
    if (e.errorCode == 2 || e.message.contains('No such file')) {
      throw const CliException(
        'Flutter SDK not found. Make sure "flutter" is on your PATH.',
        exitCode: 1,
      );
    }
    rethrow;
  }

  if (result.exitCode != 0) {
    throw FlutterCommandException(
      executable: executable,
      arguments: ['create', projectName],
      exitCode: result.exitCode,
      stdout: result.stdout.toString(),
      stderr: result.stderr.toString(),
    );
  }

  return workingDirectory != null
      ? '$workingDirectory/$projectName'
      : projectName;
}