rrousselgit / union Goto Github PK
View Code? Open in Web Editor NEWUnion types for dart
License: MIT License
Union types for dart
License: MIT License
Hi, I am facing a problem using union with generic model.
I have a base model for json operations.
abstract class BaseModel<T> {
Map<String, Object> toJson();
T fromJson(Map<String, Object> json);
}
And a response model for api
class HomeResponse extends BaseModel<HomeResponse> {
List<Data> data;
HomeResponse({this.data});
HomeResponse.fromJson(Map<String, dynamic> json) {
if (json['data'] != null) {
data = List<Data>();
json['data'].forEach((v) => data.add(Data.fromJson(v)));
}
}
@override
HomeResponse fromJson(Map<String, Object> json) {
return HomeResponse.fromJson(json);
}
@override
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class ErrorResponse {
final String description;
final String errorCode;
ErrorResponse({this.description, this.errorCode});
}
I fetch the json from ApiProvider, there is a responseModel from extend BaseModel. With this responseModel, I can make json operations(fromJson) and get the model I sent from upper layer. If success I return the object(HomeResponse) via asSecond();
class ApiProvider {
Union2<ErrorResponse, T> request<T extends BaseModel<T>>(T responseModel) {
try {
Map<String, dynamic> body = {"data": []};
T model = responseModel.fromJson(body);
return model.asSecond<ErrorResponse>();
} catch (e) {
return ErrorResponse(description: e.toString()).asFirst();
}
}
}
The method I'm calling in the upper layer
Future request() async {
Union2<ErrorResponse, HomeResponse> serverResponse = ApiProvider().request<HomeResponse>(HomeResponse());
serverResponse.switchCase(
(ErrorResponse errorResponse) => print('ErrorResponse: ${errorResponse.description}'),
(HomeResponse homeResponse) => print('HomeResponse: ${homeResponse.toString()}'),
);
}
But I get this error, why getting "BaseModel" instead of "HomeResponse" .
What am I doing wrong or how can I fix it?
type '((ErrorResponse) => void, (BaseModel<HomeResponse>) => void, Object, Object, Object, Object, Object, Object, Object) => void'
is not a subtype of
type '((ErrorResponse) => void, (HomeResponse) => void, Object, Object, Object, Object, Object, Object, Object) => void'
I'm unable to use your lib with Dart 2.9 with error:
The argument type 'TYPE_HERE' can't be assigned to the parameter type 'void Function(void Function(String), void Function(Photo), Object, Object, Object, Object, Object, Object, Object)'.
Can you confirm any related issues with this version of Dart.
Hi,
I was wondering whether you could support more than 9 cases?
I see you have some kind of generator in place.
Other libs also have a max of 9 cases.
It'd be cool to have a way to simplify unions with duplicate types.
For example:
Union3<int, int, String>
-> Union2<int, String>
.Union2<Union2<int, String>, double>
-> Union3<int, String, double>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.