Flutterfire: [cloud_firestore] Parsing error with DocumentReference

Created on 14 Feb 2020  Â·  6Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
a reference inserts in other document returns MethodChannelDocumentReference when I call from flutter.

To Reproduce
I have a document in firestore that has this structure:
user: DocumentId

  • createdAt: DateTime
  • email: String
  • coupon: Map

    • reference: DocumentReference

    • uses: Number

@override
  Future<UserModel> getUser() async {
    try {
      final authUser = await _auth.currentUser();
      if (authUser != null) {
        final user = await _firestore.document('users/${authUser.uid}').get();
        if (!user.data.containsKey('coupon') || user.data['coupon'] == null) {
          final couponCode = await _generateCode(authUser.uid);
          return UserModel.fromData(authUser, couponCode);
        } else {
          final coupon = Map<String, dynamic>.from(user.data['coupon']);
          final couponRef = coupon['reference'] as DocumentReference;  // here show me: coupon['reference'] is type MethodChannelDocumentReference is not a subtype of type 'DocumentReference' in type cast
          return UserModel.fromData(authUser, couponRef.documentID);
        }
      }
      return null;
    } catch (e) {
      log(e.toString(), error: e);
      throw ServerException();
    }
  }

so when I execute the code

 final coupon = Map<String, dynamic>.from(user.data['coupon']);
 final couponRef = coupon['reference'] as DocumentReference; 

See error or incorrect behavior
"type 'MethodChannelDocumentReference' is not a subtype of type 'DocumentReference' in type cast"

Expected behavior
I expected coupon['reference'] returns DocumentReference

Additional context
I add all coupon documents by cloudfunctions.

Packages that I use:

firebase_core: ^0.4.3+2
firebase_auth: ^0.15.3
cloud_firestore: ^0.13.2+1
cloud_functions: ^0.4.1+8

here's flutter --version

Flutter 1.12.13+hotfix.8 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 0b8abb4724 (3 days ago) • 2020-02-11 11:44:36 -0800
Engine • revision e1e6ced81d
Tools • Dart 2.7.0

flutter doctor -v

[√] Flutter (Channel stable, v1.12.13+hotfix.8, on Microsoft Windows [Versión 10.0.18362.356], locale es-PE)
    • Flutter version 1.12.13+hotfix.8 at C:\src\flutter
    • Framework revision 0b8abb4724 (3 days ago), 2020-02-11 11:44:36 -0800
    • Engine revision e1e6ced81d
    • Dart version 2.7.0


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\User\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 43.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code, 64-bit edition (version 1.42.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.8.1

[√] Connected device (1 available)
    • Redmi Note 6 Pro • 604fd608 • android-arm64 • Android 9 (API 28)

• No issues found!
bug

Most helpful comment

@deCardenas this sounds like it might have been caused by the refactor (fairly similar to the problem I experienced here: issue #1986).

I would follow the advice that @ditman gave me:

Could you test your app with cloud_firestore: 0.13.0+2 and let us know if it works? (This is so we can narrow down this as a regression of the refactor, or a preexisting bug that carried over.)

And if it still doesn't work try reverting back to cloud_firestore:0.12.11 (the latest version before the refactor of cloud_firestore) and let @ditman know what happens.

/cc @amrfarid140 might also be able to help with this.

All 6 comments

@deCardenas this sounds like it might have been caused by the refactor (fairly similar to the problem I experienced here: issue #1986).

I would follow the advice that @ditman gave me:

Could you test your app with cloud_firestore: 0.13.0+2 and let us know if it works? (This is so we can narrow down this as a regression of the refactor, or a preexisting bug that carried over.)

And if it still doesn't work try reverting back to cloud_firestore:0.12.11 (the latest version before the refactor of cloud_firestore) and let @ditman know what happens.

/cc @amrfarid140 might also be able to help with this.

@stuied148 thanks for your help.
I tried to use cloud_firestore: 0.13.0+2 and it gave me this message:
flutter_app depends on cloud_firestore 0.13.0+2 which doesn't match any versions, version solving failed
but with cloud_firestore:0.12.11 its all ok. Thanks so much! :D

@deCardenas do you mind keeping this issue open until we do some triaging.

Seems like genuine issue in the new implementation.

@amrfarid140 sure!

This issue is happening because in https://github.com/FirebaseExtended/flutterfire/blob/d5569b1238571578030dac182cf58325bea3a534/packages/cloud_firestore/cloud_firestore/lib/src/utils/codec_utility.dart#L61 we are expecting Map<String,dynamic> while it actually returns Map<dynamic,dynamic> for nested maps.

Working in a fix for this.

Was this page helpful?
0 / 5 - 0 ratings