NoSuchMethodError:创建新用户时尝试调用非函数,如 null: 'dart.global.firebase.auth'

Posted

技术标签:

【中文标题】NoSuchMethodError:创建新用户时尝试调用非函数,如 null: \'dart.global.firebase.auth\'【英文标题】:NoSuchMethodError: tried to call a non-function, such as null: 'dart.global.firebase.auth' when creating new userNoSuchMethodError:创建新用户时尝试调用非函数,如 null: 'dart.global.firebase.auth' 【发布时间】:2021-08-02 08:23:25 【问题描述】:

我一直在尝试让 firebase 身份验证在我的 Flutter Web 应用程序上运行,但是当我尝试创建新用户时,我最终遇到了以下问题。

跑线时:

UserCredential result = await _auth.signInAnonymously();

我收到以下错误:

NoSuchMethodError: 试图调用一个非函数,比如null: 'dart.global.firebase.auth'

我已经尝试按照其他人的修复程序解决相同的问题,例如将内容移到我的 index.html 文件的头部,但是到目前为止没有任何效果。

这是我的 pubspec.yaml

description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In ios, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  firebase: ^9.0.1
  firebase_storage: ^8.0.4
  
  firebase_auth: ^1.1.4
  flutter_login: ^1.0.10
  flutter_svg: ^0.19.3
  google_sign_in: ^5.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/logo.gif
  #   - images/a_dot_ham.jpeg
  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.
  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages
  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

这是我的 index.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.

    Fore more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
  -->
  <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="flutter_4paul">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

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

  <title>flutter_4paul</title>
  <link rel="manifest" href="manifest.json">


 
    <!-- 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/8.5.0/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="/__/firebase/8.5.0/firebase-auth.js"></script>
  <script src="/__/firebase/8.5.0/firebase-database.js"></script>
  
  <script>
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    var firebaseConfig = 
      //my config info
    ;
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    
  </script>
  </body>
  <script>
    if ('serviceWorker' in navigator) 
      window.addEventListener('flutter-first-frame', function () 
        navigator.serviceWorker.register('flutter_service_worker.js');
      );
    
  </script>
  <script src="main.dart.js" type="application/javascript"></script>
</head>


<!-- The core Firebase JS SDK is always required and must be listed first -->
</html>

【问题讨论】:

【参考方案1】:

我解决了这个问题,

之前

final FirebaseAuth _authr = FirebaseAuth.instance;

在我的班级顶部定义,而不是使用 _authr 我只是使用

UserCredential result = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: em, password: pass);

不完全确定这是如何修复它的,因为重新定义它并没有真正改变它......但如果它有效,它就有效!

【讨论】:

以上是关于NoSuchMethodError:创建新用户时尝试调用非函数,如 null: 'dart.global.firebase.auth'的主要内容,如果未能解决你的问题,请参考以下文章

创建Google Storage的存储对象时的NoSuchMethodError(Java)

java.lang.NoSuchMethodError: org.jaxen.dom4j.DocumentNavigator.getInstance()可能的解决办法

小吃店不工作颤振NoSuchMethodError

Dart Navigator 给出 NoSuchMethodError

颤振和json:NoSuchMethodError:方法'[]'在null上被调用

NoSuchMethodError:null 上的无效成员:'length'