Is inheritance supported? I've tried the following example but got an exception, is there something i miss?:
@JsonSerializable()
abstract class Base extends Object with _$BaseSerializerMixin {
Base(this.value);
num value;
factory Base.fromJson(Map<String, dynamic> json) => _$BaseFromJson(json);
}
@JsonSerializable()
class Sub extends Base with _$SubSerializerMixin {
Sub(num value, this.subVal): super(value);
num subVal;
factory Sub.fromJson(Map<String, dynamic> json) => _$SubFromJson(json);
}
exception thrown:
Unsupported operation: Cannot populate the required constructor argument: value.
Remove the @JsonSerialiazble annotation on Base.
No difference, still
Unsupported operation: Cannot populate the required constructor argument: value.
with:
abstract class Base extends Object with _$BaseSerializerMixin {
Base(this.value);
num value;
factory Base.fromJson(Map<String, dynamic> json) => _$BaseFromJson(json);
}
or
abstract class Base {
Base(this.value);
num value;
}
I just did this w/ the example and it worked great
abstract class ItemCore {
final int price;
ItemCore(this.price);
}
@JsonSerializable()
class Item extends ItemCore with _$ItemSerializerMixin {
int count;
int itemNumber;
bool isRushed;
Item(int price) : super(price);
factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
}
Does it work for you?
run 'pub upgrade' – which version of json_serializable are you using?
On Tue, May 8, 2018 at 11:22 AM frickt notifications@github.com wrote:
No difference, still
Unsupported operation: Cannot populate the required constructor argument:
value.with:
abstract class Base extends Object with _$BaseSerializerMixin {
Base(this.value);num value;
factory Base.fromJson(Map
json) => _$BaseFromJson(json);
}or
abstract class Base {
Base(this.value);num value;
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/json_serializable/issues/145#issuecomment-387496252,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCisYHLp2pIV_-ua0AcJ4WSDNXzqwSks5tweKDgaJpZM4T2Hen
.
Your example has the same issue:
Unsupported operation: Cannot populate the required constructor argument: price.
Maybe, the problem is flutter here, i'm using the configuration described here: https://flutter.io/json/
I did
flutter upgrade
flutter pub upgrade
both had no impact.
my yaml file looks like that:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.0
shared_preferences: 0.4.1
font_awesome_flutter: 6.0.0
json_annotation: ^0.2.2
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^0.7.6
json_serializable: ^0.3.2
That's the issue.
Look here - https://github.com/dart-lang/json_serializable/blob/master/example/pubspec.yaml
You're on old versions. I'll send a PR to fix flutter.io/json
PR to fix the site - https://github.com/flutter/website/pull/1017
Kevin thanks for your answer, it was really helpful.
I have one more question. How can we use JsonKey(name: ) in the base class properties.
I'm recieving a json with CamelCase and in my base class properties I'm using lowerCamelCase (as Dart recommends)
I'm having trouble to sort this out.
@spagni – would you copy-paste a minimal example for me?
Do you control the base class?
@kevmoo yes I control the base class. I annotated the JsonKey in the base class properties. This is what I ended up doing.
import 'package:json_annotation/json_annotation.dart';
class BaseResponse{
@JsonKey(name: 'Code')
int code;
@JsonKey(name: 'Message')
String message;
BaseResponse(this.code, this.message);
}
import 'dataContracts/base_response.dart';
import 'package:json_annotation/json_annotation.dart';
part 'login_response.g.dart';
@JsonSerializable()
class LogInResponse extends BaseResponse with _$LogInResponseSerializerMixin {
@JsonKey(name: 'ResponseObject', fromJson: _getResponseObject)
String token;
LogInResponse(int code, String message) : super(code, message);
factory LogInResponse.fromJson(Map<String, dynamic> json) => _$LogInResponseFromJson(json);
}
String _getResponseObject(Map<String, dynamic> json) {
return json['Token'];
}
What are your thoughts on this solution? Is it correct?
Thank you very much for your help
Does that seem solution seem reasonable?
On Thu, Aug 2, 2018 at 7:13 PM Sebastian notifications@github.com wrote:
@kevmoo https://github.com/kevmoo yes I control the base class. This is
what I ended up doing—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/json_serializable/issues/145#issuecomment-410123193,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCiizu4sgUllyqYKsyS6AWE-r95amlks5uM7HKgaJpZM4T2Hen
.
@kevmoo My bad, I've just updated the missing comment
What are your thoughts on this solution? Is it correct?
Looks good to me!
Most helpful comment
I just did this w/ the example and it worked great
Does it work for you?