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.62k stars 3.95k forks source link

šŸ› Error: [cloud_firestore/unknown] Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?' #5238

Closed daveparks23 closed 3 years ago

daveparks23 commented 3 years ago

Bug report

Describe the bug Upgraded to Flutter 2 and null safety: When receiving data from Firebase Firestore I get the following error "Error: [cloud_firestore/unknown] Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?'"

Steps to reproduce the behavior: Upgrade to Flutter 2.0 and migrate to null safety pub spec.yaml

name: analysis_data
description: Testing.

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  firebase_auth: ^1.0.0
  cloud_firestore: ^1.0.0
  firebase_core : ^1.0.0 
  flutter_spinkit: ^5.0.0 
  cupertino_icons: ^1.0.2
  intl: ^0.17.0
  auto_size_text: ^3.0.0-nullsafety.0
  percent_indicator: ^3.3.0-nullsafety.1

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

firebase sdk from index.html file

 <!-- The core Firebase JS SDK is always required and must be listed first -->
<script  src="https://www.gstatic.com/firebasejs/8.2.10/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="https://www.gstatic.com/firebasejs/8.2.10/firebase-auth.js"></script>
     <script  src="https://www.gstatic.com/firebasejs/8.2.10/firebase-firestore.js"></script>

Expected behavior

Receive snapshot from Firebase Firestore with no error


Additional context

My Stream Builder code

import 'package:analysis_data/screens/landing_page.dart';
import 'package:analysis_data/screens/no_permission.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import '../screens/get_user_data.dart';
import '../models/user_model.dart';

class UserService {
  //Handle User
  handleUser(String key) {
    print('signed in going for user service key: ' + key);

    return StreamBuilder(
      //initialData: null,
      stream:
          FirebaseFirestore.instance.collection('users').doc(key).snapshots(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {

        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return GettingUser();
          case ConnectionState.active:
            print('snapshot $snapshot');
            if (snapshot.hasData) {
              print('Snapshot has data.');
              DocumentSnapshot? doc = snapshot.data!;

              print('doc Id: ' + doc.id);
              print(doc.data);

              AuthUser? user = AuthUser(
                 // removed code 
                 // building local user
                return LandingPage(user: user);
              } else {
                return NoPermission();
              }
            } else if (snapshot.hasError) {
              print('Error Getting User Data: ${snapshot.error}');
              return Text('Error Getting User Data: ${snapshot.error}');
            } else {
              return GettingUser();
            }
          default:
            return GettingUser();
        }
      },
    );
  }
}

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [āœ“] Flutter (Channel stable, 2.0.1, on Mac OS X 10.15.7 19H114 darwin-x64, locale en-US) [āœ“] Android toolchain - develop for Android devices (Android SDK version 28.0.3) [āœ“] Xcode - develop for iOS and macOS [āœ“] Chrome - develop for the web [āœ“] Android Studio (version 3.5) [āœ“] VS Code (version 1.54.1) [āœ“] Connected device (1 available) ā€¢ No issues found! ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.12.0 Flutter SDK 2.0.1 analysis_data 1.0.0+1 dependencies: - auto_size_text 3.0.0-nullsafety.0 [flutter] - cloud_firestore 1.0.0 [cloud_firestore_platform_interface cloud_firestore_web firebase_core firebase_core_platform_interface flutter meta] - cupertino_icons 1.0.2 - firebase_auth 1.0.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.0.0 [firebase_core_platform_interface firebase_core_web flutter meta] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_spinkit 5.0.0 [flutter] - intl 0.17.0 [clock path] - percent_indicator 3.3.0-nullsafety.1 [flutter] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - async 2.5.0 [collection] - boolean_selector 2.1.0 [source_span string_scanner] - characters 1.1.0 - charcode 1.2.0 - clock 1.1.0 - cloud_firestore_platform_interface 4.0.0 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 1.0.0 [cloud_firestore_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.15.0 - fake_async 1.2.0 [clock collection] - firebase_auth_platform_interface 4.0.0 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 1.0.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.0.0 [flutter meta plugin_platform_interface] - firebase_core_web 1.0.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - js 0.6.3 - matcher 0.12.10 [stack_trace] - meta 1.3.0 - path 1.8.0 - plugin_platform_interface 2.0.0 [meta] - sky_engine 0.0.99 - source_span 1.8.0 [charcode collection path term_glyph] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - string_scanner 1.1.0 [charcode source_span] - term_glyph 1.2.0 - test_api 0.2.19 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0 [collection] - vector_math 2.1.0 ```

tmldoc commented 3 years ago

Having the exact same issue. Not only when getting data, regardless if future builders, streams, or simple gets. But also with sending data using update or set, it returns the exact same error but in reverse. i.e

[cloud_firestore/unknown] Expected a value of type '((Object) => Object)?', but got one of type '(Object?) => dynamic'

Which does make sense.

I appreciate everybody's efforts on this project, thanks a lot guys!ā¤ļø

boosey commented 3 years ago

Flutter Doctor output:

Doctor summary (to see all details, run flutter doctor -v): [āœ“] Flutter (Channel stable, 2.0.1, on macOS 11.2.1 20D74 darwin-x64, locale en-US) [āœ—] Android toolchain - develop for Android devices āœ— Unable to locate Android SDK. Install Android Studio from: https://developer.android.com/studio/index.html On first launch it will assist you in installing the Android SDK components. (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions). If the Android SDK has been installed to a custom location, please use flutter config --android-sdk to update to that location.

[āœ“] Xcode - develop for iOS and macOS [āœ“] Chrome - develop for the web [!] Android Studio (not installed) [āœ“] VS Code (version 1.54.1) [āœ“] Connected device (1 available)

! Doctor found issues in 2 categories.

boosey commented 3 years ago

flutter pub deps output: Dart SDK 2.12.0 Flutter SDK 2.0.1 admin 1.0.0+1

dependencies:

dev dependencies:

transitive dependencies:

Levi-Lesches commented 3 years ago

I don't know enough about this repo to say if this is the problem, but the docs say you should be on the beta channel, but it seems you're both on the stable channel. Also, it still recommends using the JS version 7.20.0, but you're using 8.2.10. Try switching those.

daveparks23 commented 3 years ago

I don't know enough about this repo to say if this is the problem, but the docs say you should be on the beta channel, but it seems you're both on the stable channel. Also, it still recommends using the JS version 7.20.0, but you're using 8.2.10. Try switching those.

Thanks! I will try this but is 7.20.0 null safe? I will try beta channel also.

boosey commented 3 years ago

I thought on Wednesday they announced general availability of Null Safety for Firestore. I know how to switch to the beta channel, but where you you specify the JS version?

tmldoc commented 3 years ago

I don't know enough about this repo to say if this is the problem, but the docs say you should be on the beta channel, but it seems you're both on the stable channel. Also, it still recommends using the JS version 7.20.0, but you're using 8.2.10. Try switching those.

Tried on stable and beta channel, same thing. I think it's an issue with firestore ^1.0.0. When I downgrade to the last version before this, it runs fine on flutter 2.0 but without sound nullsaftey of course. Tried changing the index.html firestore js from version [7.20.0-all the way up to 8.20.10]

There error displayed while sending data is:

[cloud_firestore/unknown] Expected a value of type '((Object) => Object)?', but got one of type '(Object?) => dynamic'

using cloud_firestore: ^1.0.0 firebase_core: ^1.0.0

Levi-Lesches commented 3 years ago

@daveparks23:

is 7.20.0 null safe?

Version 7.20.0 is for the JavaScript library, which doesn't have any null-safety. It's the Dart library that turns JavaScript functions into a null-safe API. Version 1.0.0 (in the pubspec.yaml) is the version that has null-safety.

@boosey

where you you specify the JS version? There are two places you need to specify packages for web, on the Dart side and the JavaScript side.

For Dart, you use pubspec.yaml:

cloud_firestore: "^1.0.0"

For JavaScript, you put this inweb/index.html:

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

I haven't tested this with null-safety myself but this is what it says on the docs, which were updated for null-safety.

Levi-Lesches commented 3 years ago

@tmldoc According to the docs:

If your app is mixing legacy and null-safe packages, use the --no-sound-null-safety flag: $ flutter run --no-sound-null-safety

Perhaps you have other packages that are not fully null-safe? Other than that, I'd say give the doc a read-over and see if everything matches.

boosey commented 3 years ago

Thanks Levi.

Its funny. I opened the exact same bug last night, so when I saw this at the top of the list this morning, I just assumed it was mind and posted the output of the diagnostic tools. Now I see it was daveparks23's issue. :-)

That being said, one of the moderators closed my issue and pointed me to an existing version of it #5050. I actually searched Issues before I opened one and found nothing. But apparently it is a known issue.

tmldoc commented 3 years ago

flutter run --no-sound-null-safety

This got it working now! But the project is a fresh and clean project, with only firebase core & firestore ^1.0.0, flutter stable 2.0.1 It works on both beta and stable channels, with firebasejs/7.20.0/firebase-firestore.js but again Running with unsound null safety.

Would gladly wait for it to run with sound null-safety. Thanks again!

Levi-Lesches commented 3 years ago

one of the moderators closed my issue and pointed me to an existing version of it #5050.

@daveparks23 So I guess this issue can be closed as a duplicate of #5050? It seems they have more info there.

daveparks23 commented 3 years ago

Duplicate Issue https://github.com/FirebaseExtended/flutterfire/issues/5050

Salakar commented 3 years ago
cloud_functions_web       4.0.1    
cloud_firestore_web       1.0.1    
cloud_firestore           1.0.1    
firebase_core_web         1.0.1    
firebase_auth_web         1.0.2    
firebase_core             1.0.1    
firebase_messaging_web    1.0.1    
firebase_storage_web      1.0.1

The above versions have been published with this fix included. Thanks @MichaelTamm for the PR.