useFirestoreEmulator method

void useFirestoreEmulator(
  1. String host,
  2. int port, {
  3. bool sslEnabled = false,
})

Changes this instance to point to a FirebaseFirestore emulator running locally.

Set the host of the local emulator, such as "localhost" Set the port of the local emulator, such as "8080" (port 8080 is default)

Note: Must be called immediately, prior to accessing FirebaseFirestore methods. Do not use with production credentials as emulator traffic is not encrypted.

Implementation

void useFirestoreEmulator(String host, int port, {bool sslEnabled = false}) {
  if (kIsWeb) {
    // use useEmulator() API for web as settings are set immediately unlike native platforms
    try {
      _delegate.useEmulator(host, port);
    } catch (e) {
      final String code = (e as dynamic).code;
      // this catches FirebaseError from web that occurs after hot reloading & hot restarting
      if (code != 'failed-precondition') {
        rethrow;
      }
    }
  } else {
    String mappedHost = host;
    // Android considers localhost as 10.0.2.2 - automatically handle this for users.
    if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
      if (mappedHost == 'localhost' || mappedHost == '127.0.0.1') {
        // ignore: avoid_print
        print('Mapping Firestore Emulator host "$mappedHost" to "10.0.2.2".');
        mappedHost = '10.0.2.2';
      }
    }

    _delegate.settings = _delegate.settings.copyWith(
      // "sslEnabled" has to be set to false for android to work
      sslEnabled: sslEnabled,
      host: '$mappedHost:$port',
    );
  }
}