首页 > 解决方案 > NoSuchMethodError: 试图调用一个非函数,比如 null: 'dart.global.firebase.auth'

问题描述

Flutter 新手 - 我怀疑在尝试设置 Firebase 身份验证时遗漏了一些非常简单的事情。

一直在网上寻找解决方案。大多数人要求您仔细检查 firebase-auth.js 是否正确包含在 index.html 文件中。这样就完成了,index.html文件在下面。

这篇文章和这篇文章表明,可以通过将我的 firebase 配置移动到 index.html 文件的 head 部分来解决这个问题。不知道为什么会这样,但尝试并没有看到任何变化。

我的 index.html 文件:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter application.">

  <!-- 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="test_auth">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="shortcut icon" type="image/png" href="favicon.png"/>

  <title>test_auth</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>
    if ('serviceWorker' in navigator) {
      window.addEventListener('load', function () {
        navigator.serviceWorker.register('flutter_service_worker.js');
      });
    }
  </script>

  <!-- Firebase Configuration-->
  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-app.js"></script>

  <!-- TODO: Add SDKs for Firebase products that you want to use
      https://firebase.google.com/docs/web/setup#available-libraries -->
  <script src="https://www.gstatic.com/firebase/7.17.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-analytics.js"></script>

  <script>
      var firebaseConfig = {
        //my deets
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
      firebase.analytics();
      firebase.auth();
      </script>
  <!-- End of Firebase Configuration-->

  <script src="main.dart.js" type="application/javascript"></script>


</body>
</html>

我的 pubspec.yaml 依赖项:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3
  firebase_core: ^0.4.5
  firebase_auth: ^0.16.1
  cloud_firestore: ^0.13.7

    flutter doctor -v
[✓] Flutter (Channel master, 1.21.0-6.0.pre.170, on Mac OS X 10.15.2 19C57, locale en-CA)
    • Flutter version 1.21.0-6.0.pre.170 at /Users/olisno/development/flutter
    • Framework revision 4c7fae93aa (6 hours ago), 2020-08-04 10:44:43 -0700
    • Engine revision 033dd45607
    • Dart version 2.10.0 (build 2.10.0-2.0.dev 0f0e04ec3a)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/olisno/Library/Android/sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.47.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.12.2

[✓] Connected device (3 available)
    • iPhone SE (2nd generation) (mobile) • 7F1F5A6D-F82D-46FB-B71B-371887713467 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-13-6 (simulator)
    • Web Server (web)                    • web-server                           • web-javascript • Flutter Tools
    • Chrome (web)                        • chrome                               • web-javascript • Google Chrome 84.0.4147.105

• No issues found!

谢谢你的任何提示!

标签: firebaseflutterfirebase-authenticationflutter-web

解决方案


就我而言,我仅针对 的版本更新了主要版本firebase_auth并解决了它。

_core 的主版本仍然是 1,但 _auth 的主版本似乎更新得比较频繁。

改为中。firebase_auth: ^1.0.1_ 好像不需要在. pub包里面会自动生成head标签。 firebase_auth: ^3.0.1pubspec.yamlweb/index.html


推荐阅读