【问题标题】:"NoSuchMethodError: tried to call a non-function, such as null: 'dart.global.firebase.auth'" when initializing auth() in flutter for web“NoSuchMethodError:尝试调用非函数,例如null:'dart.global.firebase.auth'”在flutter for web中初始化auth()时
【发布时间】:2019-11-28 14:00:53
【问题描述】:

我正在尝试在 Flutter for Web 应用程序中使用 Firebase 身份验证实现登录功能。 我已经可以使用 Firestore 在同一个应用中存储/提取数据而无需登录。

在执行应用时,显示错误“NoSuchMethodError: 试图调用非函数,例如 null: 'dart.global.firebase.auth'”

我认为错误是调用auth()方法引起的。

您能告诉我应该如何解决它吗? 或者,Flutter for web 还不支持 Firebase 身份验证吗??

进口的firebase.dart。 https://firebase.google.com/docs/reference/js/firebase

import 'package:firebase/firebase.dart';

使用 auth() 初始化一个对象,并从参数中调用登录方法。

final Auth _auth = auth();

Future<UserCredential> signIn(String email, String password) async {
  final UserCredential user = await _auth.signInAndRetrieveDataWithEmailAndPassword(email, password);
  return user;
}

【问题讨论】:

  • 您是否调试过您的应用程序以查看您传递给 _auth 的值是否确实为空?您确定您的应用程序已连接到数据库,即:app_name 中没有错误,应用程序文件夹中有 google-services.json。
  • Ryk,感谢您的评论。实际上我无法确认 _auth 是否确实为空,因为应用程序在调用auth() 时崩溃了... 有什么方法可以确认吗?是的,我可以从应用程序中使用 Firestore 获取和放置数据。我不使用 google-services.json 或 GoogleService-Info.plist,因为该应用程序适用于网络。取而代之的是,我打电话给initializeApp()。 (firebase.google.com/docs/reference/js/firebase#initialize-app)
  • 你使用来自 pub 的包吗?如果您使用来自 pub 的包 firebase,您还应该将 firebase-app.js 插入您的页面。我没有在 web 上使用 Flutter,所以我不知道在你的应用中放置脚本的位置。
  • 好的,谢谢。是的,我使用来自 pub 的包,并将 firebase-app.js 插入到我的 index.html 中。 &lt;script src="https://www.gstatic.com/firebasejs/6.2.0/firebase-app.js"&gt;&lt;/script&gt;
  • 仅供参考。我还在下面的 Reddit 中找到了一个讨论。但是我找不到我的代码和这段代码之间的区别...reddit.com/r/FlutterDev/comments/ccqgz0/…

标签: firebase flutter dart firebase-authentication flutter-web


【解决方案1】:

您也需要将其包含在您的 index.html 文件中:

<script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-auth.js"></script>

【讨论】:

  • 谢谢。这可以是解决方案。我现在不能尝试,但会尝试。
  • 这为我提供了一个修复,在插入这一行并让我的应用程序运行后,我只是阅读了前面的行,并且有一个提示,来自 Firebase 设置步骤&lt;!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries --&gt;
  • 是的。也为我解决了这个问题。
【解决方案2】:

在我的情况下,我不得不刷新缓存,用 R 重新加载/热重新加载 dart 是不够的,所以继续 chrome 并同时按 CTRL+SHIFT+R 以清除缓存。

【讨论】:

    【解决方案3】:

    对于任何可能也遇到过我的情况的人。我意识到我添加了这一行 &lt;script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"&gt;&lt;/script&gt; 两次,所以我删除了一个,保存并运行flutter clean

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2021-05-14
      • 2020-10-19
      • 2020-11-06
      • 2021-01-26
      • 2019-12-06
      • 2021-12-25
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多