updateLineInFile function

Future updateLineInFile(
  1. String filePath,
  2. String lineStartsWith,
  3. String replaceBy
)

Implementation

Future updateLineInFile(
    String filePath, String lineStartsWith, String replaceBy) async {
  final lines = await utf8.decoder
      .bind(File(filePath).openRead())
      .transform(const LineSplitter())
      .toList();

  final idx = lines.indexWhere((line) {
    return line.startsWith(lineStartsWith);
  });

  print('old "${lines[idx]}" => new : "$replaceBy"');
  lines[idx] = replaceBy;

  return File(filePath).writeAsString(lines.join('\n'));
}