ITADN

No code generated when using modified fromJson factory

#1528Openkuchienkz 创建于 2025-09-22
K
kuchienkzcommented
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<String, dynamic> json) { if (json.containsKey('firstName') { json['fullname'] = json['firstName'] + " " + json['lastName']; json.remove('firstName'); json.remove('lastName'); } return _$UserFromJson(json); } Map<String, dynamic> 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<String, dynamic> 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.
0 条评论