从 Flutter 代码向 httpsCallable 云函数传递参数

joe*_*inz 9 firebase flutter google-cloud-functions

httpsCallable使用此代码片段在 firebase 上调用云函数:

@override
  Future<InitializePickupRequestCommandResult> initialize(
    ClientEntity client,
    PositionEntity location,
    PositionEntity destination, {
    required bool isVehicleEmpty,
  }) async {
    final data = InitializePickupRequestCommand.from(
      client,
      location,
      destination,
      isVehicleEmpty: isVehicleEmpty,
    ).toJson();

    final name = describeEnum(CloudFunctionNames.initializePickupRequest);

    final initializePickupRequest = backend.httpsCallable(name);

    final result = await initializePickupRequest.call(data);

    return InitializePickupRequestCommandResult.from(
      result.data as Map<String, dynamic>,
    );
  }
Run Code Online (Sandbox Code Playgroud)

dataobject 保存 CF 执行操作所需的所有数据,它的类型为Map<String, dynamic>

 Map<String, dynamic> toJson() => {
        "clientId": clientId,
        "clientLat": clientLat,
        "clientLng": clientLng,
        "vehicleType": vehicleType,
        "isVehicleEmpty": isVehicleEmpty,
        "location": {
          "lat": clientLat,
          "lng": clientLng,
        },
        "destination": {
          "placeId": destination.id,
          "zip": destination.zip,
          "city": destination.city,
          "searchString": destination.searchString,
          "lat": destination.lat,
          "lng": destination.lng,
        },
      };
Run Code Online (Sandbox Code Playgroud)

问题

每次尝试调用 CF 时,都会抛出此异常:

_AssertionError ('package:cloud_functions/src/https_callable.dart': 断言失败: 第 33 行 pos 12: '_debugIsValidParameterType(parameters)': 不正确。)

我尝试过什么

使用这些作为参数:

  • data as Map<String, dynamic>
  • {...data}
  • <String, dynamic>{...data}

尝试{"dummy": "data"}作为参数,CF 正常执行。不知道为什么!
那么如何将参数传递给https可调用的云函数呢?

joe*_*inz 8

问题出在toJson()特别之处,"vehicleType": vehicleType,因为该值是一个枚举属性,并且引发了无效参数异常。

现在使用"vehicleType": vehicleType.index,