Flutter Web Firestore error: Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?'

663

I think this is a bug in Firebase. Until they fix it, you can try to change your local copy of firebase_core/firebase_core_web/lib/src/interop/utils/utils.dart from:

dynamic dartify(
  Object? jsObject, [
  Object Function(Object? object)? customDartify,
])

to

dynamic dartify(
  Object? jsObject, [
  Object? Function(Object object)? customDartify,
])

You will also need to remove as Object Function(Object?)?) from cloud_firestore/cloud_firestore_web/lib/src/interop/utils/utils.dart.

At least that solved the issue for me (for the moment).

Share:
663
RomaAlf
Author by

RomaAlf

Updated on December 28, 2022

Comments

  • RomaAlf
    RomaAlf over 1 year

    I simplified code for reproducing this error. It works with Android Emulator, but fails when getting collection data from Firestore, when running on Web.

    Error Message:

    Error: [cloud_firestore/unknown] Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?'
        at Object.throw_ [as throw] (http://localhost:58066/dart_sdk.js:5028:11)
        at collection_reference_web.CollectionReferenceWeb.new.get (http://localhost:58066/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:370:23)
        at get.next (<anonymous>)
        at http://localhost:58066/dart_sdk.js:37206:33
        at _RootZone.runUnary (http://localhost:58066/dart_sdk.js:37077:59)
        at _FutureListener.thenAwait.handleValue (http://localhost:58066/dart_sdk.js:32333:29)
        at handleValueCallback (http://localhost:58066/dart_sdk.js:32860:49)
        at Function._propagateToListeners (http://localhost:58066/dart_sdk.js:32898:17)
        at _Future.new.[_completeWithValue] (http://localhost:58066/dart_sdk.js:32746:23)
        at async._AsyncCallbackEntry.new.callback (http://localhost:58066/dart_sdk.js:32767:35)
        at Object._microtaskLoop (http://localhost:58066/dart_sdk.js:37329:13)
        at _startMicrotaskLoop (http://localhost:58066/dart_sdk.js:37335:13)
        at http://localhost:58066/dart_sdk.js:33106:9
    
    
    Doctor summary (to see all details, run flutter doctor -v):
    [√] Flutter (Channel beta, 1.26.0-17.6.pre, on Microsoft Windows [Version 10.0.18363.1379], locale ru-RU)
    [√] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    [√] Chrome - develop for the web
    [√] Android Studio (version 4.1.0)
    [√] IntelliJ IDEA Ultimate Edition (version 2020.1)
    [√] VS Code (version 1.53.2)
    [√] Connected device (2 available)
    

    main.dart: (MyApp() is just an empty page)

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    
    List<String> departments = List.empty(growable: true);
    
    void main() async{
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      await FirebaseAuth.instance.signInWithEmailAndPassword(email: "[email protected]", password: "111111");
      CollectionReference departmentsRef = FirebaseFirestore.instance.collection("Departments");
      QuerySnapshot qsDepartments = await departmentsRef.get(); //HERE I GET AN ERROR
      qsDepartments.docs.forEach((element) {
        Map<String, dynamic>? map = element.data();
        print(map!['name']);
        departments.add(map['name']);
      });
      runApp(MyApp());
    }
    

    web/index.html:

    <!DOCTYPE html>
    <html>
    <head>
      <base href="/">
    
      <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_web_firestore">
      <link rel="apple-touch-icon" href="icons/Icon-192.png">
    
      <!-- Favicon -->
      <link rel="icon" type="image/png" href="favicon.png"/>
    
      <title>test_web_firestore</title>
      <link rel="manifest" href="manifest.json">
    </head>
    <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>
      <!-- The core Firebase JS SDK is always required and must be listed first -->
      <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
      <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-auth.js"></script>
      <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-firestore.js"></script>
    
      <script>
      // Your web app's Firebase configuration
      var firebaseConfig = {
        apiKey: "AIzaSyA6BARqtxEwWAvYG9etXXz9iknq6PviJCU",
        authDomain: "testwebfirestore.firebaseapp.com",
        projectId: "testwebfirestore",
        storageBucket: "testwebfirestore.appspot.com",
        messagingSenderId: "604806481003",
        appId: "1:604806481003:web:9fe9c74feaceb71ee91d2e"
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
    </script>
    </body>
    </html>
    

    pubspec.yaml:

    name: test_web_firestore
    description: A new Flutter application.
    
    publish_to: 'none' # Remove this line if you wish to publish to pub.dev
    
    version: 1.0.0+1
    
    environment:
      sdk: ">=2.12.0-0 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
      firebase_core: ^0.8.0-1.0.nullsafety.1
      firebase_auth: ^0.21.0-1.1.nullsafety.1
      cloud_firestore: ^0.17.0-1.0.nullsafety.0
    
    
    
      # 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
    
    # 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
    

    When running with flutter run --no-sound-null-safety i get another error:

    Error: [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
        at Object.throw_ [as throw] (http://localhost:62031/dart_sdk.js:5331:11)
        at collection_reference_web.CollectionReferenceWeb.new.get (http://localhost:62031/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:370:23)
        at get.next (<anonymous>)
        at http://localhost:62031/dart_sdk.js:39029:33
        at _RootZone.runUnary (http://localhost:62031/dart_sdk.js:38886:58)
        at _FutureListener.thenAwait.handleValue (http://localhost:62031/dart_sdk.js:33872:29)
        at handleValueCallback (http://localhost:62031/dart_sdk.js:34432:49)
        at Function._propagateToListeners (http://localhost:62031/dart_sdk.js:34470:17)
        at _Future.new.[_completeWithValue] (http://localhost:62031/dart_sdk.js:34312:23)
        at async._AsyncCallbackEntry.new.callback (http://localhost:62031/dart_sdk.js:34335:35)
        at Object._microtaskLoop (http://localhost:62031/dart_sdk.js:39173:13)
        at _startMicrotaskLoop (http://localhost:62031/dart_sdk.js:39179:13)
        at http://localhost:62031/dart_sdk.js:34686:9