cph-cachet / research.package

A Flutter package implementing support for surveys like ResearchStack and ResearchKit
MIT License
51 stars 44 forks source link

RPInstructionStep.fromJson() throws exception #125

Closed mario-bermonti closed 1 month ago

mario-bermonti commented 2 months ago

I am getting a SerializationException when I try to build an RPInstructionStep from a json using RPInstructionStep.fromJson().

I get the error when adapting this package's example to build the RPInstructionStep used in the InformedConsentPage. You can see the code and exception below.

Is this a bug or am I misunderstanding something?

Map<String, dynamic> jsonInstruction = {
  "identifier": "instructionID",
  "title": "Welcome!",
  "detailText":
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ultricies feugiat turpis nec efficitur. Integer in pharetra libero. Proin a leo eu enim porttitor hendrerit. Suspendisse vestibulum interdum mollis. Donec in sapien ut orci ultricies laoreet. Ut maximus ante id arcu feugiat scelerisque. Proin non rutrum libero. Aliquam blandit arcu ac dolor consequat maximus. Integer et dolor quis quam tempor porta quis vel nibh. Phasellus ullamcorper fringilla lorem, ac tempus sem cursus a. Aliquam maximus facilisis quam. Morbi hendrerit tempor tellus, ac hendrerit augue tincidunt eu. Cras convallis lorem at nulla mattis tristique.",
  "footnote": "(1) Important footnote",
  "imagePath": "assets/images/waving-hand.png",
  "text": "informed_consent.welcome_text",
};

RPInstructionStep instructionStep = RPInstructionStep.fromJson(jsonInstruction);

I get the following Exception:

Exception has occurred.
SerializationException (SerializationException - A 'fromJson' function was not found in the FromJsonFactory for the type 'null'. Register a Serializable class using the 'FromJsonFactory().register()' method.
If you are using CARP Mobile Sensing, you can ensure json initialization by calling'CarpMobileSensing.ensureInitialized()' as part of your main method.)
bardram commented 2 months ago

It is because you need to initialize JSON deserialization by calling ResearchPackage.ensureInitialized().

For example, as the first thing in you main() method of your app, like this (taken from the demo app):

void main() {
  // initialize research package
  // mostly used if you load a RP configurations from a json file
  ResearchPackage.ensureInitialized();

  runApp(const RPDemoApp());
}
mario-bermonti commented 2 months ago

I am initializing JSON deserialization by calling ResearchPackage.ensureInitialized() in my main.dart. I am running the example provided by research_packages.

You can see below how my main() looks.

// main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:research_package/research_package.dart';

import 'informed_consent_page.dart';
import 'linear_survey_page.dart';
import 'navigable_survey_page.dart';

void main() {
  // initialize research package
  // mostly used if you load a RP configurations from a json file
  ResearchPackage.ensureInitialized();

  runApp(const RPDemoApp());
}
bardram commented 1 month ago

Now I see - it's because you forgot to declare the type in the json above. You need to specify the __type for the JSON deserializer to know which type to use.

So the JSON should look like this:

      Map<String, dynamic> jsonInstruction = {
        "__type": "RPInstructionStep",
        "identifier": "instructionID",
        "title": "Welcome!",
        "detailText":
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ultricies feugiat turpis nec efficitur. Integer in pharetra libero. Proin a leo eu enim porttitor hendrerit. Suspendisse vestibulum interdum mollis. Donec in sapien ut orci ultricies laoreet. Ut maximus ante id arcu feugiat scelerisque. Proin non rutrum libero. Aliquam blandit arcu ac dolor consequat maximus. Integer et dolor quis quam tempor porta quis vel nibh. Phasellus ullamcorper fringilla lorem, ac tempus sem cursus a. Aliquam maximus facilisis quam. Morbi hendrerit tempor tellus, ac hendrerit augue tincidunt eu. Cras convallis lorem at nulla mattis tristique.",
        "footnote": "(1) Important footnote",
        "imagePath": "assets/images/waving-hand.png",
        "text": "informed_consent.welcome_text",
      };

      var instructionStep = RPInstructionStep.fromJson(jsonInstruction);