why doesnt it show anything in output or debug console

this is my test file

import 'package:test/test.dart';
import 'assignment5.dart';

void main() {
  test('Serialization and deserialization', () {
    final medication1 = Medication(
      meds: "pandoil",
      dose: "200mg",
      freq: "every 4 hours",
    );

    final medication2 = Medication(
      meds: "pencillin",
      dose: "50mg",
      freq: "every 6 hours",
    );

    final patient1 = Patient(
      id: "1",
      name: "deniz",
      address: "table1",
      diagnosis: "covid",
      medications: [medication1],
    );

    final patient2 = Patient(
      id: "2",
      name: "zined",
      address: "table2",
      diagnosis: "flue",
      medications: [medication2],
    );

    final ehr = EHR();
    ehr.addPatient(patient1);
    ehr.addPatient(patient2);

    final ehrJson = ehr.toJSON();
    final newEhr = EHR.fromJSON(ehrJson);

    expect(newEhr.patients.length, equals(2));
    expect(newEhr.getPatient('1')!.name, equals('deniz'));
    expect(newEhr.getPatient('2')!.name, equals('zined'));

    final ehrXml = ehr.toXML();
    final newerEhr = EHR.fromXML(ehrXml);

    expect(newerEhr.patients.length, equals(2));
    expect(newerEhr.getPatient('1')!.name, equals('deniz'));
    expect(newerEhr.getPatient('2')!.name, equals('zined'));
  });
}

and this is my imported file

import 'dart:convert';
import 'package:xml/xml.dart' as xml;

class Medication {
  String meds;
  String dose;
  String freq;

  Medication({
    required this.meds,
    required this.dose,
    required this.freq,
  });

  String toXML() {
    final builder = xml.XmlBuilder();
    builder.element('Medication', nest: () {
      builder.element('meds', nest: meds);
      builder.element('dose', nest: dose);
      builder.element('freq', nest: freq);
    });

    return builder.buildDocument().toXmlString(pretty: true);
  }

  static Medication fromXML(String xmlString) {
    final document = xml.XmlDocument.parse(xmlString);
    final medicationElement = document.rootElement;
    return Medication(
      meds: medicationElement.findElements('meds').single.innerText,
      dose: medicationElement.findElements('dose').single.innerText,
      freq: medicationElement.findElements('freq').single.innerText,
    );
  }

  String toJSON() {
    return json.encode(toJson());
  }

  Map<String, dynamic> toJson() {
    return {
      'meds': meds,
      'dose': dose,
      'freq': freq,
    };
  }

  static Medication fromJSON(Map<String, dynamic> data) {
    return Medication(
      meds: data['meds'],
      dose: data['dose'],
      freq: data['freq'],
    );
  }
}

class Patient {
  String id;
  String name;
  String address;
  String diagnosis;
  List<Medication> medications;

  Patient({
    required this.id,
    required this.name,
    required this.address,
    required this.diagnosis,
    required this.medications,
  });

  String toXML() {
    final builder = xml.XmlBuilder();
    builder.element('Patient', nest: () {
      builder.element('id', nest: id);
      builder.element('name', nest: name);
      builder.element('address', nest: address);
      builder.element('diagnosis', nest: diagnosis);
      builder.element('medications', nest: () {
        for (final medication in medications) {
          builder.element('Medication', nest: medication.toXML());
        }
      });
    });

    return builder.buildDocument().toXmlString(pretty: true);
  }

  static Patient fromXML(String xmlString) {
    final document = xml.XmlDocument.parse(xmlString);
    final patientElement = document.rootElement;
    final medications = patientElement
        .findElements('medications')
        .single
        .findElements('Medication')
        .map((medicationElement) => Medication.fromXML(medicationElement.toXmlString()))
        .toList();

    return Patient(
      id: patientElement.findElements('id').single.innerText,
      name: patientElement.findElements('name').single.innerText,
      address: patientElement.findElements('address').single.innerText,
      diagnosis: patientElement.findElements('diagnosis').single.innerText,
      medications: medications,
    );
  }

  String toJSON() {
    return json.encode(toJson());
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'address': address,
      'diagnosis': diagnosis,
      'medications': medications.map((medication) => medication.toJson()).toList(),
    };
  }

  static Patient fromJSON(Map<String, dynamic> data) {
    final List<dynamic> medicationsData = data['medications'];
    final List<Medication> medications = medicationsData
        .map((medicationData) => Medication.fromJSON(medicationData))
        .toList();

    return Patient(
      id: data['id'],
      name: data['name'],
      address: data['address'],
      diagnosis: data['diagnosis'],
      medications: medications,
    );
  }
}

class EHR {
  Map<String, Patient> patients = {};

  String toXML() {
    final builder = xml.XmlBuilder();
    builder.element('EHR', nest: () {
      builder.element('patients', nest: () {
        for (final patient in patients.values) {
          builder.element('Patient', nest: patient.toXML());
        }
      });
    });

    return builder.buildDocument().toXmlString(pretty: true);
  }

  static EHR fromXML(String xmlString) {
    final document = xml.XmlDocument.parse(xmlString);
    final ehrElement = document.rootElement;
    final patients = ehrElement
        .findElements('patients')
        .single
        .findElements('Patient')
        .map((patientElement) => Patient.fromXML(patientElement.toXmlString()))
        .toList();

    final ehr = EHR();
    for (final patient in patients) {
      ehr.patients[patient.id] = patient;
    }

    return ehr;
  }

  String toJSON() {
    final Map<String, dynamic> data = {
      'patients': patients.map((key, value) => MapEntry(key, value.toJson())),
    };
    return json.encode(data);
  }

  static EHR fromJSON(String jsonString) {
    final Map<String, dynamic> data = json.decode(jsonString);
    final Map<String, Patient> patients = data['patients'].map<String, Patient>(
      (key, value) => MapEntry(key, Patient.fromJSON(value)),
    );
    return EHR()..patients = patients;
  }

  void addPatient(Patient patient) {
    patients[patient.id] = patient;
  }

  Patient? getPatient(String id) {
    return patients[id];
  }
}

i doesnt show anything in the debug console and i expect to get the patients names and the class created. i am trying to to serialize an object of a specific class to an XML/JSON string and deserialize a string back to an object of the right class.
I’ve created a test file which i expected to print a list of my patient and the class that ive made for them

  • i get this error btw _TypeError (type ‘(dynamic, dynamic) => MapEntry<dynamic, Patient>’ is not a subtype of type ‘(String, dynamic) => MapEntry<String, Patient>’ of ‘transform’)

    – 

  • final cannot be both to JSON and from JSON :final ehrJson = ehr.toJSON(); final newEhr = EHR.fromJSON(ehrJson); Same applies here : final ehrXml = ehr.toXML(); final newerEhr = EHR.fromXML(ehrXml);

    – 




  • So much unnecessary source code for such a simple question. For what?

    – 

Without debugPrint command the console only show if the test was ok.

Leave a Comment