Git Product home page Git Product logo

union's People

Contributors

elias8 avatar rrousselgit avatar xsahil03x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

union's Issues

Generic model error

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'

Not able to use Union with Dart 2.9

image

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.

More Cases

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.

Merge & Flatten

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>

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.