externalDataScenario function

Future<Scenario> externalDataScenario(
  1. String xml, {
  2. Map<String, String> media = const {},
  3. bool listMedia = true,
  4. PullDataInstanceAdapter? instanceAdapter,
  5. ExternalDataRepository? repository,
  6. String? instanceXml,
})

Parses the form xml with external data enabled and starts a Scenario on it.

media maps media file names (e.g. fruits.csv) to their text, served at jr://file/<name> (and jr://file-csv/<name> for CSV secondary instances). With listMedia (the default) the plugin is told the media file names, as Collect lists its media folder. With instanceXml, that saved instance is loaded (typed by externalAnswerResolver) instead of starting a new one.

Implementation

Future<Scenario> externalDataScenario(
  String xml, {
  Map<String, String> media = const {},
  bool listMedia = true,
  PullDataInstanceAdapter? instanceAdapter,
  ExternalDataRepository? repository,
  String? instanceXml,
}) async {
  final resolver = MapResourceResolver({
    for (final MapEntry(:key, :value) in media.entries) ...{
      'jr://file/$key': utf8.encode(value),
      'jr://file-csv/$key': utf8.encode(value),
    },
  });
  final plugin = ExternalDataPlugin(
    listMedia: listMedia ? (_) => media.keys : null,
    instanceAdapter: instanceAdapter,
    repository: repository == null ? null : (_) => repository,
  );
  final parser = XFormParser(resolver: resolver);
  plugin.createParseProcessors().forEach(parser.addProcessor);
  final form = await parser.parse(
    xml,
    instanceXml: instanceXml,
    answerResolver: plugin.answerResolver,
  );
  await plugin.prepareForm(form, resolver);
  return Scenario.fromFormDef(form, newInstance: instanceXml == null);
}