Giter Site home page Giter Site logo

Comments (4)

TekExplorer avatar TekExplorer commented on June 14, 2024

Note: retrofit has no way of knowing what to call feomJson on, so it would need to implement something like genericArgumentFactories where we pass in the fromJson to the method

from retrofit.dart.

trevorwang avatar trevorwang commented on June 14, 2024

There's no reflection for lutter. So, you must tell retrofit the exact type before it generates code for you. Refer ApiResult<t> for an example. @woprandi @ekuleshov

from retrofit.dart.

ekuleshov avatar ekuleshov commented on June 14, 2024

@trevorwang appreciate response. I'm aware of the issue involved with both generics and absence of reflection.

However it is a valid case to have rest api to receive or return a generic argument, when the type is not known at the compilation time. For such types the JsonSerializable can generate special to/fromJson() methods that are taking additional parameters (i.e. genericArgumentFactories option).

Currently retrofit generator is generating an invalid code like T.fromJson(_result.data!) for such classes and it doesn't seem like it allows to pass generic arg factories as code generated by the JsonSerializable is expecting.

It would be really helpful and would allow to deal with the issue without writing a bunch of boilerplate code, if we could specify additional parameters for the retrofit endpoint method, so the consuming code could provide appropriate generic factories and these factories would be wired with the to/fromJson calls generated by retrofit generator.

For example, a rest endpoint declaration could look like this:

  @POST('/api/config/save')
  @Headers({'Content-Type': 'application/json'})
  Future<Config<T>> saveConfig<T>(
          Config<T> config,
          Object? Function(T value) toJson,
          T Function(Object? json) fromJson);

Note that Config<T> class is declared like that and defined the same generic arg factories params in to/fromJson methods.

@JsonSerializable(genericArgumentFactories: true)
class Config<T> {
  ...
  T details;
  
  factory Config.fromJson(Map<String, dynamic> json, T Function(Object? json) fromJsonT)
       => _$ReportConfigFromJson(json, fromJsonT);

  Map<String, dynamic> toJson(Object? Function(T value) toJsonT) => _$ReportConfigToJson(this, toJsonT);
}

from retrofit.dart.

Raviteja11122 avatar Raviteja11122 commented on June 14, 2024

Is there any work around for this issue?

from retrofit.dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.