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

SmartStruct issue generated with freezed #87

Open
namlxcntt opened this issue Mar 31, 2023 · 3 comments
Open

SmartStruct issue generated with freezed #87

namlxcntt opened this issue Mar 31, 2023 · 3 comments

Comments

@namlxcntt
Copy link

namlxcntt commented Mar 31, 2023

Hi. I`m using Smartstruct create mapper from UserResponse -> UserModel

Version

smartstruct: ^1.4.0
smartstruct_generator: ^1.4.0
freezed_annotation: ^2.2.0
freezed: ^2.3.2

UserResponse class

`@freezed
class UserResponse with _$UserResponse {
const factory UserResponse(
{@jsonkey(name: 'id') String? id,
@jsonkey(name: 'name') String? name,
@jsonkey(name: 'phone') String? phone,
@jsonkey(name: 'email') String? email,
@jsonkey(name: 'birthday') String? birthday,
@jsonkey(name: 'avata') String? avatar,
@jsonkey(name: 'status') String? status,
@jsonkey(name: 'gender') String? gender}) = _UserResponse;

factory UserResponse.fromJson(Map<String, dynamic> json) => _$UserResponseFromJson(json);
}
`

UserModel class

@freezed class UserModel with _$UserModel { const factory UserModel( {String? id, String? name, String? phone, String? email, String? birthday, String? avatar, String? status, String? gender}) = _UserModel; }

Finally class Mapper

`part 'user_mapper.mapper.g.dart';

@Mapper()
abstract class UserMapper {
@mapping(
target: 'copyWith',
ignore: true) // drawback as freezed generates a getter for the copyWith, and we have no way of determining that the copyWith shouldn't be mapped without explicitly ignoring it
UserModel fromModel(UserResponse model);
}
`

Build error -> Bad state: No element

I'm looking for help, is there anyone who can help me?

@moloti
Copy link

moloti commented Apr 3, 2023

I have the same issue...

@moloti
Copy link

moloti commented Apr 3, 2023

Ok here is a fix, try adding a constructor:

class UserResponse with _$UserResponse {
const UserResponse._();

const factory UserResponse(
{@jsonkey(name: 'id') String? id,
@jsonkey(name: 'name') String? name,
@jsonkey(name: 'phone') String? phone,
@jsonkey(name: 'email') String? email,
@jsonkey(name: 'birthday') String? birthday,
@jsonkey(name: 'avata') String? avatar,
@jsonkey(name: 'status') String? status,
@jsonkey(name: 'gender') String? gender}) = _UserResponse;

If you do this it should work.

@namlxcntt
Copy link
Author

@moloti Oh. Wow .I think it`s work but if handle normal then using freezed does not mean anything

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

2 participants