I'm trying to generate my json helper functions for an object, that contains a list with the type of an abstract class, like this:
import 'package:json_annotation/json_annotation.dart';
import 'exercise-variations.a.dart';
part 'routine.model.g.dart';
@JsonSerializable()
class Routine {
List<ExerciseRoutine> exercises;
Routine();
factory Routine.fromJson(Map<String, dynamic> json) => _$RoutineFromJson(json);
Map<String, dynamic> toJson() => _$RoutineToJson(this);
}
import 'package:json_annotation/json_annotation.dart';
import 'exercise-variations.a.dart';
part 'base-exercise-routine.g.dart';
@JsonSerializable()
class BaseExerciseRoutine implements ExerciseRoutine {
int sets;
BaseExerciseRoutine();
factory BaseExerciseRoutine.fromJson(Map<String, dynamic> json) => _$BaseExerciseRoutineFromJson(json);
Map<String, dynamic> toJson() => _$BaseExerciseRoutineToJson(this);
}
abstract class ExerciseRoutine {}
This way I get this error:
[INFO] Running build...
[SEVERE] json_serializable:json_serializable on lib/test/routine.model.dart:
Error running JsonSerializableGenerator
Could not generate `fromJson` code for `exercises` because of type `ExerciseRoutine`.
None of the provided `TypeHelper` instances support the defined type.
package:dojohub_app_flutter/test/routine.model.dart:11:25
╷
11 │ List<ExerciseRoutine> exercises;
│ ^^^^^^^^^
╵
[INFO] Running build completed, took 1.2s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 92ms
[SEVERE] Failed after 1.3s
pub finished with exit code 1
Which makes sense, because ExerciseRoutine
does not implement a toJson
function and a fromJson
factory. Maybe I could add a toJson
function to my abstract class but how do I fix the missing fromJson
factory?