【问题标题】:type 'MethodChannelDocumentReference' is not a subtype of type 'DocumentReference' in type cast类型“MethodChannelDocumentReference”不是类型转换中“DocumentReference”类型的子类型
【发布时间】:2020-05-30 16:31:18
【问题描述】:

我正在使用 firestore 更新我的项目,这是我第一次遇到此类错误。 对我来说这很奇怪,我使用 firestore 文档所说的方式,并且在我的应用程序 Firestore 的其他部分没有任何错误, 但是在这一部分中,我使用云函数来创建一个文档并将其引用插入到其他文档中。在我需要获取 DocumentId 但它告诉我这个错误“类型'MethodChannelDocumentReference'不是类型转换中'DocumentReference'类型的子类型”之后 我留下一些截图。

这是我在云函数中的代码:

exports.generateUserCode = functions.https.onCall(async (data, context) => {
  const { uid } = data;
  let coupon = shortid.generate();
  let user = firestore.doc(`users/${uid}`);
  try {
    await firestore
      .collection("coupons")
      .doc(coupon)
      .set({
        owner: user,
        type: 0,
        value: 10,
        isPercentage: false,
        usersUsedIt: 0
      });
    await user.update({
      coupon: {
        reference: firestore.doc(`coupons/${coupon}`),
        uses: 0
      }
    });
    return {
      status: true,
      message: "codigo de invitacion generado",
      code: coupon
    };
  } catch (e) {
    console.log("error", e);
    return {
      status: false,
      message: "algo salio mal",
      code: null
    };
  }
});

这是颤振代码:

@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;
          return UserModel.fromData(authUser, couponRef.documentID);
        }
      }
      return null;
    } catch (e) {
      log(e.toString(), error: e);
      throw ServerException();
    }
  }

当我尝试调试代码时会出现这种情况:

type 'MethodChannelDocumentReference' is not a subtype of type 'DocumentReference' in type cast

【问题讨论】:

  • 在 Stack Overflow 上,请不要显示代码图片。最好将代码文本复制到问题中,以便于阅读和搜索。

标签: firebase flutter google-cloud-firestore google-cloud-functions


【解决方案1】:

solution 谢谢你的帮助。

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2021-12-30
    相关资源
    最近更新 更多