retrofit.dart icon indicating copy to clipboard operation
retrofit.dart copied to clipboard

QueryParam - optional converter option

Open moshe5745 opened this issue 3 months ago • 0 comments

Is your feature request related to a problem? Please describe. Right now to make a consistent way to convert DateTime to String we need to use our code. For instance, we need to use a helper function that converts a DateTime to a String in a specific format when we calling retrofit-generated code.

  @GET('/some-thing')
  Future<dynamic> getData(
    @Query('fromDate') String fromDate,
  );

apiClient.getData(helperConverter(DateTime.now())) 

This is not an optimal solution. Because the helper function code is not directly connected to Retrofit. So if you working in big team someone maybe wouldn’t know about the helper function or would forgot to use it.

Describe the solution you'd like To add an option of converter function to "Query" annotation.

  @GET('/some-thing')
  Future<dynamic> getData(
    @Query('fromDate', helperConverter) String fromDate,
  );

apiClient.getData(DateTime.now()) 

Describe alternatives you've considered Maybe to use new annotation like json_serializable uses.

class EpochDateTimeConverter implements JsonConverter<DateTime, int> {
  const EpochDateTimeConverter();

  @override
  DateTime fromJson(int json) => DateTime.fromMillisecondsSinceEpoch(json);

  @override
  int toJson(DateTime object) => object.millisecondsSinceEpoch;
}

  @GET('/some-thing')
  Future<dynamic> getData(
    @Query('fromDate', helperConverter) @EpochDateTimeConverter() String fromDate,
  );

moshe5745 avatar May 09 '24 10:05 moshe5745