formatDocument function

Future<void> formatDocument(
  1. String targetPath
)

Formats a Dart document using the dart format command.

Parameters:

  • targetPath: The String path to the file or directory to be formatted.

Throws:

  • Prints an error message if the formatting process fails or encounters an exception.

Implementation

Future<void> formatDocument(String targetPath) async {
  try {
    final result = await Process.run(
      'dart',
      ['format', targetPath],
      runInShell: true,
    );

    if (result.exitCode != 0) {
      print('Failed to format $targetPath: ${result.stderr}');
    }
  } catch (e) {
    print('Error while formatting $targetPath: $e');
  }
}