Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this compatible with the flutter freezed package? #29

Closed
BenjiFarquhar opened this issue Aug 26, 2021 · 5 comments
Closed

Is this compatible with the flutter freezed package? #29

BenjiFarquhar opened this issue Aug 26, 2021 · 5 comments

Comments

@BenjiFarquhar
Copy link

I haven't managed to map my freezed objects successfully. Is this possible at this stage?

@smotastic
Copy link
Owner

Hi, so I tried it and I saw 2 problems.
First is that my analyzer dependency is too old to work with the analyzer dependency of freezed.
Which is no problem, I can just update my dependency which I should do anyway.
To test it, I just used an older version of freezed which still has the old analyzer dependency (^0.14.0-nullsafety.0)

The other problem though seems to be in the implementation of my mapper.
For these models

@freezed
class Person with _$Person {
  Person._();
  factory Person(String name, int age) = _Person;
}

@freezed
class PersonModel with _$PersonModel {
  factory PersonModel(String name, int age) = _PersonModel;
}

And this mapper

@Mapper()
abstract class PersonMapper {
  Person fromModel(PersonModel model);
}

When running the build_generator I get the following error.

[SEVERE] smartstruct_generator:mapper on lib/mapper.dart:

Bad state: No element

Gonna have to check this out.
I think it has something to do with the constructor, as my mapper is just looking for the first non factory constructor.

return outputClass.constructors.where((element) => !element.isFactory).first;

I'll try to find some time in the next days

@BenjiFarquhar
Copy link
Author

Cool, thanks for looking into it :)

@andrew-tpz
Copy link

Hi, any progress with that?

I use smartstruct v1.2.4+1 and freezed: v1.0.2+1.
Mapper file is generated withput errors, but generated code is wrong, because Person is immutable.

class PersonMapperImpl extends PersonMapper {
  PersonMapperImpl() : super();

  @override
  Person fromModel(PersonModel model) {
    final person = Person();
    person.name = model.name;
    person.age = model.age;
    person.copyWith = model.copyWith;
    return person;
  }
}

@smotastic
Copy link
Owner

Hi @andrew-tpz , unfortunately not yet.
I'm still trying to maintain smartstruct, but currently, this means only reviewing open pull requests, and trying to help out as much as I can with the system in place.
I hope this will change at the beginning of next year, as I still have lots of plans for this library, but no time as always.

@smotastic
Copy link
Owner

Hi,
I merged a fix (#47), so you should be able to use freezed with smartstruct now (in the next release which will be coming soon).
As @andrew-tpz noted, the wrong constructor was used, so now the constructor with the most parameters is used. In the case of freezed, one of the given factory constructors.

Another problem was the mapping of the copyWith Method, this has to be ignored manually.
See the example on how to use freezed (https://github.com/smotastic/smartstruct/tree/develop/example/lib/freezed) or the README.

As I didn't use freezed much, I hope most of it will work now.
If you find any more problems please feel free to open another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants