addConstructorToController function

void addConstructorToController({
  1. required String path,
  2. required String providerName,
})

Implementation

void addConstructorToController({
  required String path,
  required String providerName,
}) {
  String import =
      '''import '../../../data/providers/${providerName}_provider.dart';''';
  final file = File(path);
  if (file.existsSync()) {
    final lines = file.readAsLinesSync();
    lines.insert(2, import);
    int index = lines.indexWhere((element) {
      element = element.trim();
      return element.startsWith(
          'class ${providerName.pascalCase}Controller extends GetxController');
    });
    index++;
    lines.insert(index,
        '''final ${providerName.pascalCase}Provider _${providerName}Provider;
\n
  ${providerName.pascalCase}Controller({required ${providerName.pascalCase}Provider ${providerName}Provider})
      : _${providerName}Provider = ${providerName}Provider;''');
    writeFile(file.path, lines.join('\n'), overwrite: true, logger: false);
    LogService.success(
        'The $providerName has been added to controller at path: $path\'');
  }
}