Open
Description
How to use CustomStep with json_serialization? I currently do not see anyway to parse CustomStep via json input.
I think this line is the reason.
survey_kit/lib/src/steps/step.dart
Line 31 in d0a2c79
There should be a way to add fromJson conversion for custom types I believe?
My CustomStep related code:
class CustomResult extends QuestionResult<String> {
CustomResult({
required Identifier? id,
required DateTime startDate,
required DateTime endDate,
required String? valueIdentifier,
required String? result,
required this.customData,
required this.value,
}) : super(
id: id,
startDate: startDate,
endDate: endDate,
valueIdentifier: valueIdentifier,
result: result,
);
final String customData;
final String value;
@override
List<Object?> get props => <Object?>[
id,
customData,
valueIdentifier,
startDate,
endDate,
value,
];
}
@JsonSerializable()
class CustomStep extends Step {
CustomStep({
required super.stepIdentifier,
super.isOptional = false,
super.buttonText = 'Next',
this.title = '',
this.text = '',
this.content = const SizedBox.shrink(),
required this.answerFormat,
});
@JsonKey(defaultValue: '')
final String title;
@JsonKey(defaultValue: '')
final String text;
@JsonKey(includeFromJson: false, includeToJson: false)
final Widget content;
final AnswerFormat answerFormat;
factory CustomStep.fromJson(Map<String, dynamic> json) =>
_$CustomStepFromJson(json);
@override
Map<String, dynamic> toJson() => _$CustomStepToJson(this);
@override
Widget createView({required QuestionResult? questionResult}) {
return CustomAnswerView(
questionStep: this,
result: questionResult as CustomResult?,
);
}
}
class CustomAnswerView extends StatefulWidget {
const CustomAnswerView({super.key, required this.questionStep, this.result});
final CustomStep questionStep;
final CustomResult? result;
@override
State<CustomAnswerView> createState() => _CustomAnswerViewState();
}
class _CustomAnswerViewState extends State<CustomAnswerView> {
late String? _result;
@override
void initState() {
super.initState();
_result = 'Adarsh';
}
@override
Widget build(BuildContext context) {
return StepView(
step: widget.questionStep,
resultFunction: () => CustomResult(
id: widget.questionStep.stepIdentifier,
startDate: DateTime.now(),
endDate: DateTime.now(),
valueIdentifier: _result.toString(),
result: _result,
customData: 'some custom data',
value: 'some value',
),
isValid: widget.questionStep.isOptional || _result != null,
title: widget.questionStep.title.isNotEmpty
? Text(
widget.questionStep.title,
style: Theme.of(context).textTheme.displayMedium,
textAlign: TextAlign.center,
)
: widget.questionStep.content,
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(bottom: 14.0),
child: Text(
widget.questionStep.text,
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
),
],
),
);
}
}
Not sure how to make Task object creation acknowledge this custom steop from json?
Future<Task> getJsonTask() async {
try {
final String taskJson =
await rootBundle.loadString('assets/example_json.json');
final Map<String, dynamic> taskMap = json.decode(taskJson); // this does not recognize custom step
return Task.fromJson(taskMap);
} catch (e) {
rethrow;
}
}
Metadata
Metadata
Assignees
Labels
No labels