添加依赖项 cloud_firestore 时,新的 Flutter 应用程序失败:^1.0.7

Posted

技术标签:

【中文标题】添加依赖项 cloud_firestore 时,新的 Flutter 应用程序失败:^1.0.7【英文标题】:New Flutter App fails when adding dependency cloud_firestore: ^1.0.7 【发布时间】:2021-07-24 06:30:20 【问题描述】:

我按照这些步骤创建了一个新的 Flutter 应用并添加了 cloud_firestore 依赖项

    我在我的 MacOS Big Sur 11.2.3 上设置了颤振

    我在命令行中输入:flutter create test_dependencies

    dart migrate --apply-changes

    flutter run -> 该应用程序作为网络应用程序运行良好。 chrome 浏览器打开应用程序,我可以使用计数器

    我跑flutter pub add cloud_firestore

    我确认在 pubspec.yaml 中添加了 cloud_firestore: ^1.0.7

    然后我更新了所有依赖项和 index.html,如下所示

    我跑flutter packages get

    我跑flutter run

现在我收到此错误。我实际上有一个更大的应用程序,我从这个简单的场景开始来隔离问题。这是我能写的最简单的应用程序,但它仍然失败。任何帮助将不胜感激。

TypeError:无法读取未定义的属性“app” 在 Object.app$ [作为应用程序] (http://localhost:64162/packages/firebase_core_web/src/interop/core.dart.lib.js:42:101) 在新的 cloud_firestore_web.FirebaseFirestoreWeb.new (http://localhost:64162/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:865:64) 在 Function.registerWith (http://localhost:64162/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:788:73) 在 Object.registerPlugins (http://localhost:64162/packages/blackdoor/generated_plugin_registrant.dart.lib.js:33:46) 在主要(http://localhost:64162/web_entrypoint.dart.lib.js:41:35) 在 main.next () 在 runBody (http://localhost:64162/dart_sdk.js:37228:34) 在 Object._async [as async] (http://localhost:64162/dart_sdk.js:37259:7) 在 main$ (http://localhost:64162/web_entrypoint.dart.lib.js:40:18) 在 http://localhost:64162/main_module.bootstrap.js:19:10 在 Array.forEach () 在 window.$dartRunMain (http://localhost:64162/main_module.bootstrap.js:18:32) 在:1:8 在 Object.runMain (http://localhost:64162/dwds/src/injected/client.js:8656:21) 在 http://localhost:64162/dwds/src/injected/client.js:22068:19 在 _wrapJsFunctionForAsync_closure.$protected (http://localhost:64162/dwds/src/injected/client.js:3830:15) 在 _wrapJsFunctionForAsync_closure.call$2 (http://localhost:64162/dwds/src/injected/client.js:10905:12) 在 Object._asyncStartSync (http://localhost:64162/dwds/src/injected/client.js:3794:20) 在 main__closure1.$call$body$main__closure (http://localhost:64162/dwds/src/injected/client.js:22080:16) 在 main__closure1.call$1 (http://localhost:64162/dwds/src/injected/client.js:22007:19) 在 StaticClosure._rootRunUnary [as call$2$5] (http://localhost:64162/dwds/src/injected/client.js:4153:16) 在 _CustomZone.runUnary$2$2 (http://localhost:64162/dwds/src/injected/client.js:12136:39) 在 _CustomZone.runUnaryGuarded$1$2 (http://localhost:64162/dwds/src/injected/client.js:12068:14) 在 _ControllerSubscription._sendData$1 (http://localhost:64162/dwds/src/injected/client.js:11697:19) 在 _DelayedData.perform$1 (http://localhost:64162/dwds/src/injected/client.js:11849:59) 在 _PendingEvents_schedule_closure.call$0 (http://localhost:64162/dwds/src/injected/client.js:11898:14) 在 Object._microtaskLoop (http://localhost:64162/dwds/src/injected/client.js:3990:24) 在 StaticClosure._startMicrotaskLoop (http://localhost:64162/dwds/src/injected/client.js:3996:11) 在 _AsyncRun__initializeScheduleImmediate_internalCallback.call$1 (http://localhost:64162/dwds/src/injected/client.js:10774:9) 在 invokeClosure (http://localhost:64162/dwds/src/injected/client.js:1250:26) 在突变观察者。 (http://localhost:64162/dwds/src/injected/client.js:1269:18)

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.
    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="An apiro 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="blackdoor">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">
  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>
  <title>blackdoor</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('flutter-first-frame', function () 
        navigator.serviceWorker.register('flutter_service_worker.js?v=1463819943');
      );
    
  </script>
  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-storage.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.1.2/firebase-messaging.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/firebasejs/8.4.3/firebase-analytics.js"></script>
  <script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = 
    apiKey: "***",
    authDomain: "***",
    projectId: "***",
    storageBucket: "***",
    messagingSenderId: "***",
    appId: "***",
    measurementId: "***"
  ;
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>
  <script src="main.dart.js" type="application/javascript"></script>
</body>
</html>

pubspec.yaml

name: blackdoor
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.12.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations: 
    sdk: flutter         
  provider: ^5.0.0
  intl: ^0.17.0
  http: ^0.13.1
  image_picker: ^0.7.4

  cloud_firestore: ^1.0.7
  cloud_firestore_web: ^1.0.7
  firebase_auth: ^1.1.2
  firebase_auth_web: ^1.1.0
  firebase_core: ^1.1.0
  firebase_core_web: ^1.0.2
  firebase_messaging: ^9.1.3
  firebase_messaging_web: ^1.0.6
  firebase_storage: ^8.0.5
  firebase_storage_web: ^1.0.5

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.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:
  generate: true # Enable generate flag for localisation  

  # 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:
  #   - images/a_dot_burr.jpeg
  #   - 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: OpenSans
      fonts:
        - asset: assets/fonts/OpenSans-Regular.ttf
        - asset: assets/fonts/OpenSans-Bold.ttf
          weight: 700

    - family: Quicksand
      fonts:
        - asset: assets/fonts/Quicksand-Regular.ttf
        - asset: assets/fonts/Quicksand-Bold.ttf
          weight: 700
  #   - 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

【问题讨论】:

在 android/app/build.gradle 的底部添加这个暂时解决了我的问题,也许它也对你有用:apply plugin: 'com.google.gms.google-services' com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true 您发布的错误日志很可能是由构建 web 版本的 Flutter 生成的,而不是 Android 或 iOS。因为那时您会看到平台异常。你能仔细检查一下吗?这也是因为您可能有一个自动生成的 webpluginregistrant.dart 文件,并且它缺少一些核心 Web 依赖项。 @HuthaifaMuayyad 是的,你是对的,我正在构建 Flutter 的 Web 版本。我不知道我需要添加哪些依赖项 查看我的回答here,如果不能解决问题,请分享您的pubspec.yaml和index.html,我们可以解决。 您能否将 index.html 和 pubspec.yaml 作为您问题的补充,您可以编辑您的帖子并添加项目。它会更容易阅读,没有字符限制 【参考方案1】:

截至 2021 年 5 月 1 日

将这些依赖项添加到您的 pubspec.yaml:

  cloud_firestore: ^1.0.7
  cloud_firestore_web: ^1.0.7
  firebase_auth: ^1.1.2
  firebase_auth_web: ^1.1.0
  firebase_core: ^1.1.0
  firebase_core_web: ^1.0.2
  firebase_messaging: ^9.1.3
  firebase_messaging_web: ^1.0.6
  firebase_storage: ^8.0.5
  firebase_storage_web: ^1.0.5

【讨论】:

以上是关于添加依赖项 cloud_firestore 时,新的 Flutter 应用程序失败:^1.0.7的主要内容,如果未能解决你的问题,请参考以下文章

未找到模块“cloud_firestore”

将 cloud_firestore 添加到 pubspec Flutter 后无法为模拟器构建应用程序

添加 Firestore 依赖项会导致 Flutter 应用程序出错

在 `.symlinks/plugins/cloud_firestore/ios` 中找不到 `cloud_firestore` 的 podspec

pom.xml 每次添加新依赖时都会出错

添加依赖项时如何自动更新主解决方案?