11

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?

4
  • 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. Commented Aug 14, 2021 at 19:21
  • 2
    Another thing: try jsonEncode(data) before submitting. Commented Aug 14, 2021 at 19:45
  • @PeterKoltai your last comment led to the answer below. Commented Aug 15, 2021 at 8:29
  • @joe_izn Great! Commented Aug 15, 2021 at 8:31

1 Answer 1

12

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,

Sign up to request clarification or add additional context in comments.

1 Comment

Same thing happened to me. It was a parameter that was a Duration. Basically, if you get this, check all your parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.