Passing parameters to httpsCallable cloud function from Flutter code

411

The problem was in toJson() specifically in "vehicleType": vehicleType, because the value is an enum property and that what was throwing the invalid parameters exception.

Now using"vehicleType": vehicleType.index,

Share:
411
joe_inz
Author by

joe_inz

"No one in the brief history of computing has ever written a piece of perfect software. It's unlikely that you'll be the first." - Andy Hunt

Updated on December 31, 2022

Comments

  • joe_inz
    joe_inz over 1 year

    Using this code snippet to call httpsCallable cloud function on 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>,
        );
      }
    

    data object holds all required data for the CF to perform the operation, it is of type 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,
            },
          };
    

    Problem

    every time when trying to call the CF, it throws this exception:

    _AssertionError ('package:cloud_functions/src/https_callable.dart': Failed assertion: line 33 pos 12: '_debugIsValidParameterType(parameters)': is not true.)

    What i tried

    using these as params:

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

    Tried {"dummy": "data"} as a param and the CF was executed normally. don't know why!
    So how parameters should be passed to https callable cloud function?

    • Peter Koltai
      Peter Koltai over 2 years
      I am not sure but maybe the last commas are the problem when there is nothing after them, like "lng": clientLng,, "lng": destination.lng, and the last },. Try removing them. It's ok in Flutter but maybe it means an invalid JSON.
    • Peter Koltai
      Peter Koltai over 2 years
      Another thing: try jsonEncode(data) before submitting.
    • joe_inz
      joe_inz over 2 years
      @PeterKoltai your last comment led to the answer below.
    • Peter Koltai
      Peter Koltai over 2 years
      @joe_izn Great!