useFirestoreEmulator method

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

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,
  bool automaticHostMapping = true,
}) {
  if (kIsWeb) {
    // use useEmulator() API for web as settings are set immediately unlike native platforms
    try {
      _delegate.useEmulator(host, port);
    } catch (e) {
      // We convert to string to be compatible with Flutter <= 3.7 and Flutter >= 3.10
      // .code is only available in Flutter <= 3.7
      String strError = e.toString();

      // this catches FirebaseError from web that occurs after hot reloading & hot restarting
      if (!strError.contains('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') &&
          automaticHostMapping) {
        // 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',
    );
  }
}