firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.72k stars 3.97k forks source link

πŸ› [cloud_firestore] Uncaught TypeError: Cannot read properties of undefined (reading 'apply') #9106

Closed ViniciusSossela closed 2 years ago

ViniciusSossela commented 2 years ago

Bug report

Some firestore method/execution on web are trowing the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
    at Object.bX (main.dart.js:9188:23)
    at aB6.a2m (main.dart.js:83433:16)
    at cyX.$1 (main.dart.js:83482:23)
    at Object.cyU (main.dart.js:15840:18)
    at cyW.$2 (main.dart.js:95452:21)
    at k9.O (main.dart.js:68411:18)
    at Object.bU (main.dart.js:54616:24)
    at Object.cyU (main.dart.js:15838:3)
    at Object.rx (main.dart.js:12531:10)
    at bSW.eP (main.dart.js:83588:6)

This only happens after deploying on web, in debug everything is fine;

Steps to reproduce

 firebase_core: ^1.18.0
  cloud_firestore: ^3.1.18

Additional context


Already have notice that this error happens when using firestore batch. Example:

final _fireBatch = _firestore.batch();
final _ref = _firestore
    .collection('xxxx')
    .doc(xx)
    .collection(xxx);

for (var centerCost in itens) {
  _fireBatch.set(_ref.doc(), entity.toJson());
}

await _fireBatch.commit();

Flutter doctor

Run flutter doctor and paste the output below:

Doctor summary (to see all details, run flutter doctor -v): [βœ“] Flutter (Channel unknown, 3.0.5, on macOS 12.1 21C52 darwin-x64, locale en-BR) [βœ“] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) [βœ“] Xcode - develop for iOS and macOS (Xcode 13.3) [βœ“] Chrome - develop for the web [βœ“] Android Studio (version 2021.1) [βœ“] VS Code (version 1.69.1) [βœ“] Connected device (2 available) [βœ“] HTTP Host Availability

β€’ No issues found!

darshankawar commented 2 years ago

Thanks for the report @ViniciusSossela Can you upgrade your firebase plugins to latest and try again ?

cloud_firestore: ^3.3.0
firebase_core: ^1.19.2
https://pub.dev/packages/cloud_firestore_web/install

Also, can you provide index.html along with a minimal code sample that triggers this error ?

ViniciusSossela commented 2 years ago

Hi @darshankawar thanks to reply.

Just looked at my pub.lock:

 cloud_firestore_web:
    dependency: transitive
    description:
      name: cloud_firestore_web
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.7.0"

....
firebase_core:
    dependency: "direct main"
    description:
      name: firebase_core
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.19.2"

So basically it was using the last firebase core version, but I also made test with the latest firebase versions and the same happens.

Here are the 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="XXXXXX">

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

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

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

  <script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXX"></script>

  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>
</html>

and here the main.dart firebase initializer:

  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
darshankawar commented 2 years ago

Can you try adding below imports in index file and see if they help ?

<script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js"></script>

ViniciusSossela commented 2 years ago

@darshankawar Just added as you suggested, but the same happens:

 <script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXX"></script>
  <script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js"></script>

  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>

The errors that happens with the scripts on index.html

Uncaught SyntaxError: Unexpected token 'export' (at VM104 firebase-app.js:2350:1)

VM105 firebase-firestore.js:1 Uncaught SyntaxError: Cannot use import statement outside a module (at VM105 firebase-firestore.js:1:1)

main.dart.js:4951 Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
    at Object.bX (main.dart.js:9188:23)
    at aB6.a2m (main.dart.js:83433:16)
    at cyX.$1 (main.dart.js:83482:23)
    at Object.cyU (main.dart.js:15840:18)
    at cyW.$2 (main.dart.js:95452:21)
    at k9.O (main.dart.js:68411:18)
    at Object.bU (main.dart.js:54616:24)
    at Object.cyU (main.dart.js:15838:3)
    at Object.rx (main.dart.js:12531:10)
    at bSW.eP (main.dart.js:83588:6)
ViniciusSossela commented 2 years ago

It seems a problem with the firebase_core_web 1.7.0

Solved the issue by downgrading to the following package version:

firebase_core: 1.13.1
firebase_core_web: 1.6.1
darshankawar commented 2 years ago

Thanks for the update. Using below dependency plugin and trying to run plugin's example on web, I am getting another error which is pointing to irebase_core_web 1.7.0:

  firebase_core: ^1.19.2
  cloud_firestore: ^3.3.0

Error: Assertion failed: file:///Users/dhs/Documents/Fluttersdk/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.0/lib/src/firebase_core_web.dart:207:11

Labeling it based on the report.

binlebin commented 2 years ago

I have the same error: working fine in debug mode but error in deployment.

Lyokone commented 2 years ago

I just tried batch on debug and release mode and works fine for both. Do you have the error with batch too @binlebin ?

binlebin commented 2 years ago

@Lyokone I did get the bug on a simple get query in deployment only. Sample code below:

Query query = postsRef.orderBy('created_time', descending: true); if (pageKey != null) { query = query.startAfterDocument(_postToSnapshotMap[pageKey.uniqueCode]!); }

query = query.where('followers', arrayContains: _user!.uid); query = query.limit(_pageSize);

if (!isAdmin) { query = query.where('blocked', isEqualTo: false); }

print('Constructed query $query');

QuerySnapshot querySnapshot = await query.get();

print('Query completed');

In debug mode, everything works fine. In deployment, the last print statement was never reached. It seems query.get() is where the bug is.

I needed to downgrade all firestore packages:

pub outdated output:

Package Name Current Upgradable Resolvable Latest

direct dependencies: cloud_firestore 3.2.1 3.4.0 3.4.0 3.4.0
cloud_functions
3.3.1 3.3.3 3.3.3 3.3.3
firebase_analytics 9.1.12 9.2.1 9.2.1 9.2.1
firebase_app_check
0.0.6+16 0.0.6+18 0.0.6+18 0.0.6+18
firebase_auth 3.4.1 3.5.0 3.5.0 3.5.0
firebase_core
1.19.1 1.20.0 1.20.0 1.20.0
firebase_messaging 11.4.4 11.4.4 12.0.1 12.0.1
firebase_storage *10.3.1 10.3.3 10.3.3 10.3.3

flutter doctor -v

[βœ“] Flutter (Channel stable, 3.0.5, on macOS 12.4 21F79 darwin-arm, locale en-US) β€’ Flutter version 3.0.5 at /Users/anh/Dev/flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision f1875d570e (9 days ago), 2022-07-13 11:24:16 -0700 β€’ Engine revision e85ea0e79c β€’ Dart version 2.17.6 β€’ DevTools version 2.12.2

[βœ“] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) β€’ Android SDK at /Users/anh/Library/Android/sdk β€’ Platform android-32, build-tools 32.1.0-rc1 β€’ Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java β€’ Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) β€’ All Android licenses accepted.

[βœ“] Xcode - develop for iOS and macOS (Xcode 13.4.1) β€’ Xcode at /Applications/Xcode.app/Contents/Developer β€’ CocoaPods version 1.11.3

[βœ“] Chrome - develop for the web β€’ Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[βœ“] Android Studio (version 2021.2) β€’ Android Studio at /Applications/Android Studio.app/Contents β€’ Flutter plugin can be installed from: πŸ”¨ https://plugins.jetbrains.com/plugin/9212-flutter β€’ Dart plugin can be installed from: πŸ”¨ https://plugins.jetbrains.com/plugin/6351-dart β€’ Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[βœ“] VS Code (version 1.69.1) β€’ VS Code at /Applications/Visual Studio Code.app/Contents β€’ Flutter extension can be installed from: πŸ”¨ https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[βœ“] Connected device (3 available) β€’ Anh’s iPhone X (mobile) β€’ 1db0cd86988ec78b646876c35013e9e345a5dcd8 β€’ ios β€’ iOS 15.5 19F77 β€’ macOS (desktop) β€’ macos β€’ darwin-arm64 β€’ macOS 12.4 21F79 darwin-arm β€’ Chrome (web) β€’ chrome β€’ web-javascript β€’ Google Chrome 103.0.5060.134

[βœ“] HTTP Host Availability β€’ All required HTTP hosts are available

m-j-g commented 2 years ago

I'm in dependency hell trying to get around this issue.

If I try to downgrade firebase_auth to 3.4.1 I get:

../flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.4.1/lib/src/recaptcha_verifier.dart:57:27: Error: Required named parameter 'auth' must be provided. _factory.delegateFor( ^

If I upgrade firebase_auth to 3.4.2 or higher than it requires firebase_core_web: 1.7 . If I upgrade firebase_core_web 1.7 then I inherit the issues listed here. What is the fix?

m-j-g commented 2 years ago

I fixed one 3.4.1 auth issue with https://github.com/firebase/flutterfire/issues/9216, but now I just have more issues:

Target dart2js failed: Exception: Warning: The 'dart2js' entrypoint script is deprecated, please use 'dart compile js' instead. ../flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-2.6.19/lib/cloud_firestore_web.dart:101:14: Error: The method 'FirebaseFirestoreWeb.runTransaction' has fewer named arguments than those of overridden method 'FirebaseFirestorePlatform.runTransaction'. Future<T?> runTransaction(TransactionHandler transactionHandler, ^

This just seems like a bottomless pit of issues trying to downgrade various things. I really can't figure out the right combinations. The one listed above causes errors.

I'm in a bind because we just released our new mobile version last night, and I had to put the web update or else the two wouldn't play well together. However, as mentioned here, I have functionality that works in debug, but breaks on release build, and so I have full features that just don't load. This issue has been open for 11 days which leads me to believe that this isn't urgent. I just am at a loss here, I have been working on this all day and only running into more and more issues as I try to fix this original issue. This is supposed to be the stable branch!

Lyokone commented 2 years ago

You might want to run flutter clean on your project. As you can see on pub.dev, the latest version of cloud_firestore_web is 2.8.1 : https://pub.dev/packages/cloud_firestore_web

m-j-g commented 2 years ago

You might want to run flutter clean on your project. As you can see on pub.dev, the latest version of cloud_firestore_web is 2.8.1 : https://pub.dev/packages/cloud_firestore_web

Yeah, I'm on my 100th flutter clean today. And yes, I wish the latest versions didn't have these issues so that we could use them.

Lyokone commented 2 years ago

I've tried all the combinations that you provided @binlebin and they all work in debug and release on the Example app of FlutterFire. Can anyone provide a full repro example so we can fix this quickly?

ViniciusSossela commented 2 years ago

@Lyokone

Which firebase_core_web version are you using?

m-j-g commented 2 years ago

For what it's worth. I get this error when I use the Query option "startAfterDocument()" in my query. Like everyone else says, this works find in debug builds, but gives the mentioned error message in release builds.

ViniciusSossela commented 2 years ago

@m-j-g did you tried firebase_core_web: 1.6.1 ?

binlebin commented 2 years ago

I also notice the error might be because of "startAfterDocument()" as my first page loads fine.

@m-j-g You might want to reuse old pubspec.lock of a version that was working fine.

m-j-g commented 2 years ago

@m-j-g did you tried firebase_core_web: 1.6.1 ?

I've been trying so hard to use it. haha. But it requires me to downgrade the auth library, and then it starts a bunch of other dependency / outdated issues.

I also notice the error might be because of "startAfterDocument()" as my first page loads fine.

@m-j-g You might want to reuse old pubspec.lock of a version that was working fine.

That's a good point. I have the same experience, initial list data loads fine, but when I query for more using startAfterDocument() the error happens. (only on web release, once again works fine on web debug and on iOS/android)

ViniciusSossela commented 2 years ago

@m-j-g Yeah, I got the same. This bug got me crazy man....

Did you also tried the new version firebase_core_web: 1.7.1

m-j-g commented 2 years ago

@m-j-g Yeah, I got the same. This bug got me crazy man....

Did you also tried the new version firebase_core_web: 1.7.1

Yeah, still has the issue.

ViniciusSossela commented 2 years ago

@m-j-g try all these firebase dependencies..

  firebase_core: ^1.19.1
  firebase_core_web: 1.6.6
  cloud_firestore: ^3.2.1
  cloud_firestore_platform_interface: 5.5.10
  firebase_auth: ^3.4.1
  firebase_auth_platform_interface: 6.3.1
  firebase_storage: ^10.3.1
  firebase_analytics: ^9.1.12
  firebase_analytics_platform_interface: 3.1.10
  firebase_crashlytics: ^2.8.2
  firebase_messaging: ^11.4.4
  firebase_remote_config: ^0.10.0+4
  firebase_in_app_messaging: ^0.5.0+8

To me it worked.

m-j-g commented 2 years ago

@m-j-g try all these firebase dependencies..

  firebase_core: ^1.19.1
  firebase_core_web: 1.6.6
  cloud_firestore: ^3.2.1
  cloud_firestore_platform_interface: 5.5.10
  firebase_auth: ^3.4.1
  firebase_auth_platform_interface: 6.3.1
  firebase_storage: ^10.3.1
  firebase_analytics: ^9.1.12
  firebase_analytics_platform_interface: 3.1.10
  firebase_crashlytics: ^2.8.2
  firebase_messaging: ^11.4.4
  firebase_remote_config: ^0.10.0+4
  firebase_in_app_messaging: ^0.5.0+8

To me it worked.

It works! Thanks man, I really appreciate that.

Lyokone commented 2 years ago

Hey, I can finally reproduce the error, thanks @binlebin, I'll check what can cause the compilation in JS not to work properly

Lyokone commented 2 years ago

We have raised the issue internally with Google to see what happens with dart2js. We'll keep you posted here. Even if it's not ideal, you can always flutter build --debug in the time being if you need some functionalities from the latest version.

Lyokone commented 2 years ago

It was fixed with 3.6.1

LAPPIT commented 2 years ago

Tested the new version and it worked fine :) Thanks!

bottle-rocket-11 commented 2 years ago

3.6.1 fixed the auth persistence bug. I am still experiencing the Uncaught TypeError: Cannot read properties of undefined (reading 'apply') in production. Works fine in debug

shadowboxingskills commented 2 years ago

+1. production build still breaks due to this undefined (reading 'apply') bug

ViniciusSossela commented 2 years ago

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

The fix seems to be in the cloud_firestore https://github.com/firebase/flutterfire/pull/9246/files


@Lyokone Have this PR also fixed the problem with batch transactions ?

final _fireBatch = _firestore.batch();
final _ref = _firestore
    .collection('xxxx')
    .doc(xx)
    .collection(xxx);

for (var item in itens) {
  _fireBatch.set(_ref.doc(), entity.toJson());
}

await _fireBatch.commit();
shadowboxingskills commented 2 years ago

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

Yes, upgraded to these latest versions above but crash still occurs in production (not in debug)

bottle-rocket-11 commented 2 years ago

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

Yes, I get the same as @shadowboxingskills the crash still happens in production with all those version but works fine in debug

shadowboxingskills commented 2 years ago

It has been fixed in the latest release earlier today. Thank you