Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to httpsCallable cloud function from Flutter code

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?

like image 800
joe_inz Avatar asked Apr 01 '26 11:04

joe_inz


1 Answers

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,

like image 118
joe_inz Avatar answered Apr 09 '26 17:04

joe_inz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!