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!