Closed mario-bermonti closed 1 month 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());
}
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());
}
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);
I am getting a
SerializationException
when I try to build an RPInstructionStep from a json usingRPInstructionStep.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?
I get the following
Exception
: