Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Custom Step with json serialiaztion #141

Open
adar2378 opened this issue Jan 4, 2024 · 1 comment
Open

How to use Custom Step with json serialiaztion #141

adar2378 opened this issue Jan 4, 2024 · 1 comment

Comments

@adar2378
Copy link
Contributor

adar2378 commented Jan 4, 2024

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.

factory Step.fromJson(Map<String, dynamic> json) {

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;
    }
  }
@adar2378 adar2378 changed the title Custom Step with json serialiaztion How to use Custom Step with json serialiaztion Jan 4, 2024
@almogtovim
Copy link

Hey, Did you manage to figure it up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants