It is possible using build.yaml, refer to this answer
Step 1:
create build.yaml, place in root project folder
targets:
$default:
builders:
source_gen:combining_builder:
options:
build_extensions:
'^lib/classes/{{}}.dart': 'lib/generated/{{}}.g.dart'
Step 2:
- inside lib/classes , create your model annotated with @JsonSeriazable(), i will give a bit example
import 'package:json_annotation/json_annotation.dart';
part '../generated/product.g.dart'; // generated file will stored inside lib/generated folder
@JsonSerializable()
class Product {
final int? id;
final String? name;
final int? price;
Product({
this.id,
this.name,
this.price,
});
factory Product.fromJson(Map<String, dynamic> json) =>
_$ProductFromJson(json);
Map<String, dynamic> toJson() => _$ProductToJson(this);
}
Step 3:
flutter pub run build_runner watch --delete-conflicting-outputs