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

[Feature Request] provide field externally in fromJson method. #1131

Open
matanshukry opened this issue Apr 16, 2022 · 1 comment
Open

[Feature Request] provide field externally in fromJson method. #1131

matanshukry opened this issue Apr 16, 2022 · 1 comment

Comments

@matanshukry
Copy link

Proposal

Mark a field that should be used as an input in toJson, rather than read from the jsonMap.

Example:

@JsonSerializable()
class Person {
  @JsonKey(ignore: true, externalFromJson: true)
  final String id;

  final String firstName;

  Person({required this.id, required this.firstName});

  /// Connect the generated [_$PersonFromJson] function to the `fromJson`
  /// factory.
  factory Person.fromJson(String id, Map<String, dynamic> json) => _$PersonFromJson(id, json);

  /// Connect the generated [_$PersonToJson] function to the `toJson` method.
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}

The generatd code of fromJson would then use the argument rather than attempting to read it from the json map.

Use Case

Firebase documents. Each document in firebase has an id, and it's saved externally from the json. Right now there are various options to overcome this, but they all involve some change to the way the data should be represented, only to conform to the libraries we use - which shouldn't be true.

to clarify, the changes I'm talking about is changing the type of the id. Either removing final, adding late, changing to nullable String?, etc.

Assuming we're using Firebase and we have a document

@Nico04
Copy link

Nico04 commented Mar 5, 2023

Great idea, I'd love this for the same Firebase purpose.
Looks similar to this feature request.

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