TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)

Posted

技术标签:

【中文标题】TypeError: Cannot read properties of undefined (reading \'app\') (Flutter web app)【英文标题】:TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app) 【发布时间】:2021-12-11 21:07:18 【问题描述】:

我尝试将我的网络应用程序连接到 Firestore 并尝试运行我的网络应用程序超过 3 天,但我不能。请帮助我解决我的问题并更快地发展我的创业公司。 显示如下错误:TypeError: Cannot read properties of undefined (reading 'app') 我该如何解决这个问题?

我的 Flutter (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="jaitun_admin">
      <link rel="apple-touch-icon" href="icons/Icon-192.png">
    
      <title>jaitun_admin</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 src="https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js"></script>
      <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-analytics.js"></script>
      <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-firestore.js"></script>
      <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-auth.js"></script>
      
    
      <script>
    
    const firebaseConfig = 
      apiKey: "AIzaSyDmv2oj8958mbUg07FW316IxOAiOdYhPTg",
      authDomain: "jaituncustomaer.firebaseapp.com",
      databaseURL: "https://jaituncustomaer-default-rtdb.firebaseio.com",
      projectId: "jaituncustomaer",
      storageBucket: "jaituncustomaer.appspot.com",
      messagingSenderId: "771594380318",
      appId: "1:771594380318:web:aa0a3b61483d19dc2bfa0f",
      measurementId: "G-B064R6Q9GJ"
    ;
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    </script>
      <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>
    </body>
    </html>

【问题讨论】:

这是我的 pubspec.yml 版本:1.0.0+1 环境:sdk:">=2.3.0 cupertino_icons: ^1.0.2 firebase_core: ^1.3.0 cloud_firestore: ^2.2.2 ars_dialog: ^1.0.8 alert_dialog: ^1.0.0 firebase_auth: ^1.4.1 flutter_admin_scaffold: ^0.0.5 firebase: ^9.0.1 firebase_storage: ^8.1.3 chips_choice: ^2.0.1 flutter_switch: ^0.2.1 email_validator: ^1.0.6 Code samples 应该是minimal、完整和有代表性的。与问题相关的信息应编辑到问题中,而不是留在 cmets 中。 【参考方案1】:

Sol.1

在 firebase 初始化时。 你想用什么作为你的公共目录?

使用 build/web 而不是 public 作为公共目录。

如果还是不行... Sol.2 将 index.html 中的基本 href 链接 ($FLUTTER_BASE_HREF) 替换为存储库的位置。 &lt;base href="$FLUTTER_BASE_HREF"&gt;&lt;base href="/"&gt;&lt;base href="/your-repository/"&gt;

右键单击并查看页面源以了解想法。

【讨论】:

以上是关于TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)

TypeError: Cannot read properties of undefined (reading 'length') - 想要解释为啥代码这样说

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'fingerprint') Laravel Live

D3.js 一直遇到这个错误 TypeError: Cannot read properties of null (reading 'ownerDocument')?

React Material UI:Appbar 给出了 TypeError: Cannot read properties of undefined (reading '100')

TypeError: Cannot read property 'scrollIntoView' of null 如何解决?