We know we can format the dart code by command line like:
https://dart.dev/tools/dart-format
but how to format dart file by dart code?
like DartFormater.format(str);
CodePudding user response:
The dart format
command line tool uses the dart_style package.
You can add the package as a dependency in your pubspec.yaml
.
dependencies:
dart_style: ^2.2.4
You can use the formatter in your code like so:
import 'package:dart_style/dart_style.dart';
const String str = '''
void main( ) {
print
('hello world!'
);
}
''';
void main() {
print(DartFormatter().format(str));
}
This will print out the formatted string below:
void main() {
print('hello world!');
}