google / google/json_serializable.dart

No code generated when using modified fromJson factory

Open
#1,528 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Since there is a need to convert older version data model (which saved locally), I customized the `fromJson` factory as follows:

```
import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable(nullable: false)
class User {
// final String firstName; //deprecated
// final String lastName; //deprecated
final String fullname;

User({this.fullname});

factory User.fromJson(Map json) {
if (json.containsKey('firstName') {
json['fullname'] = json['firstName'] + " " + json['lastName'];
json.remove('firstName');
json.remove('lastName');
}

return _$UserFromJson(json);
}

Map toJson() => _$UserToJson(this);
}
```

With the above code, `build_runner` does not generate the `*.g.dart` file. But when I changed the `fromJson` like the usual:

```
...
factory User.fromJson(Map json) => _$UserFromJson(json);
...
```

...the `*.g.dart` file is generated.

Im quite new with flutter/dart but I think the custom fromJson should have work. Did I miss something?

Also, what I've tried:
Running with `clean` first, did not work.
Running with `--verbose` does give any error message.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.