ServiceAccount.fromFile constructor

ServiceAccount.fromFile(
  1. File file
)

Creates a ServiceAccount using a service account .json file.

If the transferred file does not exist, a FileNotFoundException is thrown.

If the transferred file does not have a Json extension a UnsupportedFileExtensionException is thrown. This is done to eliminate a possible source of error and to make sure that the passed file is really a Json file.

Implementation

factory ServiceAccount.fromFile(File file) {
  // Make sure that the passed file is not null and the file exists.
  if (!file.existsSync()) throw FileNotFoundException(file);
  // Make sure that the transferred file is really a Json file.
  if (!file.path.contains('.json')) {
    throw UnsupportedFileExtensionException(file);
  }
  final json = file.readAsStringSync();
  return ServiceAccount.fromString(json);
}