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

FromJson(json) #7

Closed
smiLLe opened this issue Feb 9, 2020 · 2 comments
Closed

FromJson(json) #7

smiLLe opened this issue Feb 9, 2020 · 2 comments

Comments

@smiLLe
Copy link
Contributor

smiLLe commented Feb 9, 2020

Hi Remi,

great work, again :)
FromJson(json) will generate code that requires a property called runtimeType in my map.

Planets _$PlanetsFromJson(Map<String, dynamic> json) {
  assert(json['runtimeType'] is String);
  switch (json['runtimeType'] as String) {
    case 'default':
      return _Planets.fromJson(json);

    default:
      throw FallThroughError();
  }
}

Am i supposed to add runtimeType to every Map ?
Imagine the following response:

{
	"count": 1,
	"results": [
		{
			"name": "Alderaan"
                }
         ]
}

2 Maps, 2 Models.

Planets {
  List<Planet> planets
}

Planet {
 String name
}
@smiLLe
Copy link
Contributor Author

smiLLe commented Feb 9, 2020

These are my models

@immutable
abstract class Planets with _$Planets {
  factory Planets(List<Planet> results) = _Planets;

  factory Planets.fromJson(Map<String, dynamic> json) =>
      _$PlanetsFromJson(json);
}

@immutable
abstract class Planet with _$Planet {
  factory Planet(String name) = _Planet;

  factory Planet.fromJson(Map<String, dynamic> json) => _$PlanetFromJson(json);
}

@rrousselGit
Copy link
Owner

Ah, that runtimeType is for unions (to know which constructor to use)

I'll update the code to work without it for classes with a single constructor

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