Issue
I was trying to set firebase for and I follow all the procedures. But After running the app it went all white and showed this error
‘TypeError: Cannot read property ‘app’ of undefined’ In Flutter Web
I then went on to google to search for solution and did everything the way they asked but I ended up having the null safety error. After that I remove the null safety and this is now the error I am getting
Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
Debug service listening on ws://127.0.0.1:57090/YI8fSoqgm_w=/ws
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
at Object.u [as app] (https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js:1:18836)
at Object.app$ [as app] (http://localhost:57026/packages/firebase_core_web/src/interop/core.dart.lib.js:31:101)
at new cloud_firestore_web.FirebaseFirestoreWeb.new (http://localhost:57026/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:934:64)
at Function.registerWith (http://localhost:57026/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:834:73)
at Object.registerPlugins (http://localhost:57026/packages/tembea/generated_plugin_registrant.dart.lib.js:18:46)
at main (http://localhost:57026/web_entrypoint.dart.lib.js:31:35)
at main.next (<anonymous>)
at runBody (http://localhost:57026/dart_sdk.js:40666:34)
at Object._async [as async] (http://localhost:57026/dart_sdk.js:40697:7)
at main$ (http://localhost:57026/web_entrypoint.dart.lib.js:30:18)
at http://localhost:57026/main_module.bootstrap.js:19:10
at Array.forEach (<anonymous>)
at window.$dartRunMain (http://localhost:57026/main_module.bootstrap.js:18:32)
at <anonymous>:1:8
at Object.runMain (http://localhost:57026/dwds/src/injected/client.js:8452:21)
at http://localhost:57026/dwds/src/injected/client.js:24130:19
at _wrapJsFunctionForAsync_closure.$protected (http://localhost:57026/dwds/src/injected/client.js:4461:15)
at _wrapJsFunctionForAsync_closure.call$2 (http://localhost:57026/dwds/src/injected/client.js:11511:12)
at Object._asyncStartSync (http://localhost:57026/dwds/src/injected/client.js:4425:20)
at main__closure3.$call$body$main__closure (http://localhost:57026/dwds/src/injected/client.js:24142:16)
at main__closure3.call$1 (http://localhost:57026/dwds/src/injected/client.js:24069:19)
at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
at _ForwardingStreamSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
at _ForwardingStreamSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
at _ForwardingStreamSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12695:12)
at _MapStream._handleData$2 (http://localhost:57026/dwds/src/injected/client.js:12756:12)
at _ForwardingStreamSubscription._handleData$1 (http://localhost:57026/dwds/src/injected/client.js:12721:20)
at Object.eval (eval at Closure_forwardCallTo (http://localhost:57026/dwds/src/injected/client.js:1701:14), <anonymous>:3:44)
at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
at _ControllerSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
at _ControllerSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
at _SyncStreamController._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12201:32)
at _SyncStreamController.add$1 (http://localhost:57026/dwds/src/injected/client.js:12082:15)
at Object.eval (eval at Closure_forwardInterceptedCallTo (http://localhost:57026/dwds/src/injected/client.js:1781:14), <anonymous>:3:45)
at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
at _ControllerSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
at _ControllerSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
at _SyncStreamController._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12201:32)
at _SyncStreamController.add$1 (http://localhost:57026/dwds/src/injected/client.js:12082:15)
at _GuaranteeSink.add$1 (http://localhost:57026/dwds/src/injected/client.js:23581:25)
at HtmlWebSocketChannel_closure1.call$1 (http://localhost:57026/dwds/src/injected/client.js:23874:60)
at _EventStreamSubscription_closure.call$1 (http://localhost:57026/dwds/src/injected/client.js:18042:26)
at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4829:16)
at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
at _CustomZone_bindUnaryCallbackGuarded_closure.call$1 (http://localhost:57026/dwds/src/injected/client.js:13016:25)
at invokeClosure (http://localhost:57026/dwds/src/injected/client.js:1524:26)
at WebSocket.<anonymous> (http://localhost:57026/dwds/src/injected/client.js:1543:18)
Below is my main function
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
And the pubspec
environment: sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
flutter_signin_button: ^2.0.0
firebase_auth: ^3.1.4
firebase_core: ^1.8.0
cloud_firestore: ^2.5.4
provider: ^6.0.1
firebase_analytics: ^7.0.1
Finally the index.html
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="tembea">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<title>tembea</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.2.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.2.0/firebase-analytics.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyAQYkB8TMTJKMmdzmjVQwsyiQrfMaXqOao",
authDomain: "tembea-4d3c6.firebaseapp.com",
projectId: "tembea-4d3c6",
storageBucket: "tembea-4d3c6.appspot.com",
messagingSenderId: "909705170922",
appId: "1:909705170922:web:d034fefe33c9d0d2026c93",
measurementId: "G-11NNW6J10N"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
</script>
</body>
</html>
I do not know what I may have done wrong. Any help would be welcomed.
Solution
You have two seperate imports for Firebase, which may cause issues. Remove your <script type="module">... import...</script>
and replace it with the different style of initialization like this:
<script>
const firebaseConfig = {
apiKey: "AIzaSyAQYkB8TMTJKMmdzmjVQwsyiQrfMaXqOao",
authDomain: "tembea-4d3c6.firebaseapp.com",
projectId: "tembea-4d3c6",
storageBucket: "tembea-4d3c6.appspot.com",
messagingSenderId: "909705170922",
appId: "1:909705170922:web:d034fefe33c9d0d2026c93",
measurementId: "G-11NNW6J10N"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
This is currently recommended way that can be found in Flutter Firebase docs.
Since you are initializing Firebase in the main function you may also want to move your app startup script below the script of Firebase initialization.
Answered By - pr0gramist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.