markgravity / object_mapper

Object Mapper for Flutter
8 stars 3 forks source link

Generic Class is not defined in Reflection.factories #3

Open rasyadh opened 3 years ago

rasyadh commented 3 years ago

I have class model BaseResponses

import 'package:object_mapper/object_mapper.dart';

class BaseResponses<T> with Mappable {
  String message;
  T data;

  @override
  void mapping(Mapper map) {
    map('message', message, (v) => message = v);
    map<T>('data', data, (v) => data = v);
  }
}

and also register it in mappable factories

Mappable.factories = {
  BaseResponses: () => BaseResponses(),
};

I try to convert json to BaseResponses which T is another class model, on this case is Customer. But when I try to call it like this:

BaseResponses<T> baseResponses = Mapper.fromJson(responseData).toObject<BaseResponses<T>>();

I got an error its throw execution error with message:

BaseResponses<Customer> is not defined in Reflection.factories

markgravity commented 3 years ago

@rasyadh please, also adding your Customer into factories and mixin it with Mappable

rasyadh commented 3 years ago

@markgravity already adding it, but still got the same error

markgravity commented 3 years ago

@rasyadh oh, I found the problem. When we define like this

Mappable.factories = {
  BaseResponses: () => BaseResponses(),
};

it means

Mappable.factories = {
  BaseResponses: () => BaseResponses<dynamic>(),
};

so that it will throw the error. I will look further for a better solution for your case

rasyadh commented 3 years ago

@markgravity thank you, I think it will be great if we can do so I will try to do work around on this as well

MrPlancton commented 1 year ago

Any solution? :(