So I was running my application fine as a Chrome
instance with the code below.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Check if the app is running on the web
if (kIsWeb) {
print("Running on Web - Initializing Firebase...");
try {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print("Firebase initialized successfully for Web.");
} catch (e) {
print("Error initializing Firebase for Web: $e");
}
} else {
print("Not running on Web - Firebase functionality disabled.");
}
runApp(
DevicePreview(
enabled: !kReleaseMode, // Enabled only in development mode
builder: (context) => const MyApp(), // Wrap your app
),
);
}
Then, I decided to add a Firebase Test Suit
and modified my code, following the documentation:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (kIsWeb || Platform.isIOS || Platform.isAndroid) {
print("Running on Web/iOS/Android - Initializing Firebase...");
if (kDebugMode) {
try {
FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
print(
"Firebase initialized successfully - DEVELOPMENT - for Web/iOS/Android.");
} catch (e) {
print(e);
}
} else {
try {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print(
"Firebase initialized successfully - PRODUCTION - for Web/iOS/Android.");
} catch (e) {
print(e);
}
}
} else {
print("Not running on Web/iOS/Android - Firebase functionality disabled.");
}
runApp(
DevicePreview(
enabled: !kReleaseMode, // Enabled only in development mode
builder: (context) => const MyApp(), // Wrap your app
),
);
}
and now, in the IDE I get no errors and it should be running fine, but I get this kind of error, and I am not sure how to fix it:
Exception has occurred.
TypeError: Cannot read properties of undefined (reading 'getApp')
packages/firebase_core_web/src/interop/core.dart 59:28 app$
packages/firebase_core_web/src/firebase_core_web.dart 360:48 <fn>
packages/firebase_core_web/src/firebase_core_web.dart 384:21 guardNotInitialized
packages/firebase_core_web/src/firebase_core_web.dart 360:13 app
packages/firebase_core/src/firebase.dart 79:41 app
packages/cloud_firestore/src/firestore.dart 33:21 get instance
packages/secret_flutter_project/main.dart 62:36 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 610:19 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 634:23 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 532:3 _asyncStartSync
packages/secret_flutter_project/main.dart 54:6 main$
web_entrypoint.dart 24:31 <fn>
lib/ui_web/ui_web/initialization.dart 41:15 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 610:19 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 634:23 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 581:31 <fn>
dart-sdk/lib/async/zone.dart 1676:54 runUnary
dart-sdk/lib/async/future_impl.dart 204:18 handleValue
dart-sdk/lib/async/future_impl.dart 902:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 931:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 707:5 [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 777:7 callback
dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 186:7 <fn>
Can somebody help me resolve this issue, please? I suspect it has something to do with those libraries, but not sure