Comments (4)
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.
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.
@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.
Is there any work around for this issue?
from retrofit.dart.
Related Issues (20)
- Various issues HOT 1
- Retrofit does not skip null parameters when I use pojo to bundle the query param HOT 1
- How to remove "baseUrl" in my ApiService.g to disable the hint HOT 2
- dart_mappable for type conversion HOT 2
- Warning "Use 'const' for final variables initialized to a constant value" in Generated Code HOT 2
- please use analyzer 6.0 HOT 2
- Does retrofitGenerator not support multipartFile? HOT 2
- Migrate to the official package "logging" for logging
- how do we send PartMap as in Android lib? Or how can I set dynamic name for each file in the list of files? HOT 1
- to GET a List of objects, do i really have to create an extra dummy class and convert later to a List again? HOT 4
- Extend Retrofit & Freezed Serialization with Custom fromJson for Generic Types HOT 26
- [ Change some URLs at execution ] HOT 5
- [retrofit_generator] Null check operator used on a null value HOT 1
- [retrofit_generator] Null check operator used on a null value again HOT 3
- please fix returning http code 302 HOT 1
- [Question] How to get various models from one retrofit client? HOT 2
- Support record returns HOT 3
- Add methods to the API class HOT 2
- only debug mode occur error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from retrofit.dart.