4

I am trying to use firebase emulator, it all works out, but not for the storage emulator.

Below is the code i am using, i have tried 10.0.2.2 as well but stil the same error.

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 and authentication
Future _connectToFirebaseEmulator() async {
  final localHost = Platform.isAndroid ? '127.0.0.1' : 'localhost';

  // FirebaseFirestore.instance.settings = Settings(
  //     host: '$localHost:8080', sslEnabled: false, persistenceEnabled: false);

  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 get this 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".

So it looks like the flutter is setting it up correctly. But when i try to load an image from the storage, i get.

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

If i click on the link, i do see the image in my browser.

I have made sure that i can read and write by changing the storage.rules file

BTW i am using cached_network_image, perhaps it's that one that can handle the emulator?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Martin
  • 101
  • 6
  • Does not matter, got the same error if i use Image.network – Martin Sep 11 '21 at 17:14
  • 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 }, – Hesam Jan 19 '22 at 19:42
  • Same error here, I find there is a lack of guides on how to use firebase storage emulator. – Alex Stroescu Jul 16 '23 at 11:54

0 Answers0