【问题标题】:Flutter cloud_functions Invalid argument: Instance of 'GeoPoint'Flutter cloud_functions无效参数:'GeoPoint'的实例
【发布时间】:2020-04-20 17:31:14
【问题描述】:

您好,我在 Flutter 中尝试运行可调用的 firebase 函数时遇到此错误。我不知道原因的根源,任何帮助将不胜感激。

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
    functionName: 'firebaseFunction',
);

dynamic resp = await callable.call(<String, dynamic>{
    "geoPoint" : geoPoint, //This is a variable with firestore class type GeoPoint
});

返回错误

未处理的异常:无效参数:“GeoPoint”实例

【问题讨论】:

    标签: firebase flutter dart google-cloud-functions


    【解决方案1】:

    正如错误所说,您不能将 geoPoint 的实例作为值传递给方法 call,因为它不受支持。根据文档,支持以下内容:

    /// Executes this Callable HTTPS trigger asynchronously.
      ///
      /// The data passed into the trigger can be any of the following types:
      ///
      /// `null`
      /// `String`
      /// `num`
      /// [List], where the contained objects are also one of these types.
      /// [Map], where the values are also one of these types.
      ///
      /// The request to the Cloud Functions backend made by this method
      /// automatically includes a Firebase Instance ID token to identify the app
      /// instance. If a user is logged in with Firebase Auth, an auth ID token for
      /// the user is also automatically included
    

    https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_functions/lib/src/https_callable.dart

    例子:

    dynamic resp = await callable.call(<String, dynamic>{
        'name': 'peter',
    });
    

    【讨论】:

    • 谢谢,这非常有帮助,但是如何将时间戳、地理位置等数据传递到数据中。我应该将 nums 传递给函数以使函数将它们转换为此类
    • @GILO Flutter 中的调用方法,它调用 android 中的 call 方法 firebase.google.com/docs/reference/android/com/google/firebase/… 你可以在 url 中看到它作为参数的内容
    【解决方案2】:

    感谢 Peter Haddad 指出错误的根本原因。

    只能使用 null、String、num、List 或 Map 类型

    我使用的方法是这样的。在颤抖中

    dynamic resp = await callable.call(<String, dynamic>{
      ///I split geopoint into its latitude and longitude
      "lat" : geoPoint.latitude,
      "long" : geoPoint.longitude,
    
      ///I split timestamp into its seconds and nanoseconds
      "seconds" : timestamp.seconds,
      "nanoseconds" : timestamp.nanoseconds,
    });
    

    我将地理点分为其纬度和经度。通过将时间戳分为秒和纳秒,这也适用于时间戳。然后在 firebase 函数中,我收集了数据并使用 admin.firestore.XXX() 重建了类

    const lat = data["lat"];
    const long = data["long"];
    const geoPoint = new admin.firestore.GeoPoint(lat, long);
    
    const seconds = data["seconds"];
    const nanoseconds = data["nanoseconds"];
    const timeStamp = new admin.firestore.Timestamp(seconds, nanoseconds);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2019-05-14
      • 2019-02-27
      • 1970-01-01
      相关资源
      最近更新 更多