Firebase Storage Emulator Connection Refused Error in Android Emulator
Asked Answered
T

1

6

I am trying to use the Firebase Emulator Suite for my Flutter application. While Firestore, Authentication, and Functions emulators work correctly, I am encountering issues with the Storage emulator.

Below is the code I'm using. I have tried using 10.0.2.2 instead of 127.0.0.1, but the same error persists.

void main() async {
  Bloc.observer = AppBlocObserver();

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  if (USE_EMULATOR) {
    await _connectToFirebaseEmulator();
  }

  runZonedGuarded(() {
    runApp(ElectronicCupongs(appTitle: 'LunchBrejk'));
  }, (error, stacktrace) {
    FirebaseCrashlytics.instance.recordError(error, stacktrace);
  });
}

/// Connect to Firebase emulator
Future _connectToFirebaseEmulator() async {
  final localHost = Platform.isAndroid ? '10.0.2.2' : 'localhost';

  FirebaseFirestore.instance.useFirestoreEmulator(localHost, 8080);
  FirebaseStorage.instance.useStorageEmulator(localHost, 9199);
  FirebaseFunctions.instance.useFunctionsEmulator(localHost, 5001);
  await FirebaseAuth.instance.useAuthEmulator(localHost, 9099);
}

When Flutter starts, I see the following in the debug console:

I/flutter ( 7233): Mapping Firestore Emulator host "127.0.0.1" to "10.0.2.2".
I/flutter ( 7233): Mapping Storage Emulator host "127.0.0.1" to "10.0.2.2".
I/flutter ( 7233): Mapping Functions Emulator host "127.0.0.1" to "10.0.2.2".
I/flutter ( 7233): Mapping Auth Emulator host "127.0.0.1" to "10.0.2.2".

However, when I try to load an image from storage, I get the following error:

I/flutter ( 7233): CacheManager: Failed to download file from http://localhost:9199/v0/b/default-bucket/o/restaurants%2FR81.jpg?alt=media&token=4983e466-53d1-4f4e-a1d4-09cce1b27ba1 with error:
I/flutter ( 7233): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 39308

Interestingly, I can access the image through the browser using the URL provided in the error message.

To ensure it's not a rules issue, I have modified my storage.rules file to allow read and write access.

I am using the cached_network_image package. Could it be that cached_network_image cannot handle the emulator URLs properly?

Question: How can I resolve the "Connection refused" error when using the Firebase Storage emulator in my Flutter app? Any suggestions or insights on what might be causing this issue?

Thank you for your help!

Tomcat answered 11/9, 2021 at 17:10 Comment(5)
Does not matter, got the same error if i use Image.networkTomcat
Please put your firebase.json file here, too. Make sure you are setting the host in addition to the port. Something like this: "storage": { "host": "0.0.0.0", "port": 9199 },Stopgap
Same error here, I find there is a lack of guides on how to use firebase storage emulator.Hesper
I have the same error, did you manage to fix it ?Unweighed
@Maxime, No I havn't. As of now the project is beeing rebuilt in ASP.NET core for different reasons.Tomcat
S
1

Android emulator uses the port 10.0.2.2 as localhost:

The address 127.0.0.1 on your development machine corresponds to the emulator's loopback interface. To access services running on your development machine loopback interface, use the special address 10.0.2.2 instead.[1]

Therefore, a simple workaround is to replace the localhost 127.0.0.1 with 10.0.2.2 before loading the image:

final String imageUrl = "http://127.0.0.1:9199/...";

if (UniversalPlatform.isAndroid && kDebugMode)
  imageUrl = imageUrl.replaceAll("127.0.0.1", "10.0.2.2");

return CachedNetworkImage(imageUrl: imageUrl);
//or
return Image.network(imageUrl);

[1] https://developer.android.com/studio/run/emulator-networking#networkaddresses

Solander answered 23/5, 2024 at 9:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.