initFirestore function

Future<FirestoreRepo> initFirestore({
  1. required String project,
  2. required String database,
  3. required String usage,
})

Implementation

Future<FirestoreRepo> initFirestore({
  required String project,
  required String database,
  required String usage,
}) async {
  final firestore = di<FirestoreRepo>();

  try {
    await firestore.init(projectId: project, databaseId: database);
  } on Exception catch (error, stackTrace) {
    log.info('Firestore init error=$error', stackTrace);

    final envVars = Platform.environment;
    final credentialsFile = envVars['GOOGLE_APPLICATION_CREDENTIALS'];
    if (credentialsFile == null || credentialsFile.isEmpty) {
      throw UsageException(
        '''
Have you forgotten to set the GOOGLE_APPLICATION_CREDENTIALS environment variable?

More about here: $kFirestoreCredentialsHelpUrl''',
        '',
      );
    } else {
      throw UsageException(
        '''
Check your internet connection or credentials file

More about credentials file: $kFirestoreCredentialsHelpUrl''',
        '',
      );
    }
  }

  return firestore;
}