addDependencyToBinding function

void addDependencyToBinding(
  1. String path,
  2. String providerName
)

Implementation

void addDependencyToBinding(String path, String providerName) {
  String import =
      '''import '../../../data/providers/${providerName}_provider.dart';''';
  var file = File(path);
  if (file.existsSync()) {
    var lines = file.readAsLinesSync();
    lines.insert(2, import);
    var index = lines.indexWhere((element) {
      element = element.trim();
      return element.startsWith('void dependencies() {');
    });
    index++;
    lines.insert(index, '''Get.lazyPut<${providerName.pascalCase}Provider>(
          () => ${providerName.pascalCase}Provider(
            restClientDio: Get.find(),
          ),
);''');
    final indexController = lines.indexWhere((element) {
      element = element.trim();
      return element.startsWith('() => ${providerName.pascalCase}Controller()');
    });
    lines[indexController] = '''() => ${providerName.pascalCase}Controller(
        ${providerName}Provider: Get.find(),
      ),''';
    writeFile(file.path, lines.join('\n'), overwrite: true, logger: false);
    LogService.success(
        'The $providerName has been added to binding at path: $path\'');
  }
}