readFile static method

String readFile(
  1. String filePath, {
  2. Encoding encoding = utf8,
  3. dynamic saveMode = false,
})

Implementation

static String readFile(String filePath,
    {Encoding encoding = utf8, saveMode = false}) {
  String? text = null;
  try {
    text = File(filePath).readAsStringSync(encoding: encoding);
    if (saveMode && text.isEmpty) {
      // if it is empty, set to null and try other ways
      text = null;
    }
  } on Exception catch (e, s) {
    text = null;
  }
  if (text == null) {
    for (var tryEncoding in fallBackEncodings) {
      try {
        text = File(filePath).readAsStringSync(encoding: tryEncoding);
        if (text.isNotEmpty) {
          break;
        }
      } on Exception catch (e, s) {
        // ignore
      }
    }
  }
  return text!;
}