import 'dart:convert'; import 'package:fhir/r4.dart'; import 'package:flutter/material.dart'; import 'package:flutter_fhir_new/main.dart'; import 'package:http/http.dart' as http; class Fhir extends StatelessWidget { const Fhir({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Fhir'), ), body: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Center( child: ElevatedButton( onPressed: () async { //creo observation var observation = Observation( resourceType: R4ResourceType.Observation, status: ObservationStatus.final_, /*category: [ CodeableConcept(coding: [ Coding(system: FhirUri('prova'), code: Code("prova")) ], text: "prova") ],*/ code: CodeableConcept( coding: [Coding( system: FhirUri('https://www.snomed.org/'), code: Code('000000'), display: "Walking Test")], ), subject: Reference( reference: "Patient/2936332", //luca rossi ), effectiveDateTime: FhirDateTime("2020-07-29T00:02:00+01:00"), //se si vuole un performer bisogna creare la risorsa nel fhir server /*performer: [ Reference( reference: "Practitioner/e1e35745-5403-4a83-904f-4401baa25e10", display: "") ],*/ bodySite: CodeableConcept(coding: [ Coding(display: 'Ankle'), //bodysite Coding(display: 'Left') //side ]), //possibilità di inserire note /*note: [ Annotation( authorReference: Reference( reference: "Practitioner/e1e35745-5403-4a83-904f-4401baa25e10", display: "Prandin R."), time: FhirDateTime("2020-07-29T00:02:00+01:00"), text: Markdown('Prova')) ],*/ component: [ ObservationComponent( valueString: 'prova', code: CodeableConcept(coding: [ Coding( display: "Prova", system: FhirUri("http://snomed.info/sct")), ], text: "Prova")) ]); var response = await http.post(Uri.parse('$server/Observation'), headers: headers, body: jsonEncode(observation.toJson())); if (response.statusCode == 200 || response.statusCode == 201) { Observation body = Observation.fromJson(jsonDecode(response.body)); showDialog( context: context, builder: (ctx) => AlertDialog( title: Text( 'Observation successfully sent!\nId: ${body.id}'), content: SingleChildScrollView(child: Text(response.body)), actions: [ TextButton( onPressed: () { Navigator.of(ctx).pop(); }, child: const Text("Ok"), ), ], ), ); } else { showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('Error'), content: SingleChildScrollView(child: Text(response.body)), actions: [ TextButton( onPressed: () { Navigator.of(ctx).pop(); }, child: const Text("Ok"), ), ], ), ); } }, child: const Text('Create Observation'), ), ), Center( child: ElevatedButton( onPressed: () async { var response = await http.get( Uri.parse('$server/Patient/2936332'), headers: headers); Patient pat = Patient.fromJson(jsonDecode(response.body)); showDialog( context: context, builder: (ctx) => AlertDialog( title: Text( 'Patient ${pat.name!.first.family} ${pat.name!.first.given!.first}'), content: SingleChildScrollView(child: Text(response.body)), actions: [ TextButton( onPressed: () { Navigator.of(ctx).pop(); }, child: const Text("Ok"), ), ], ), ); /*Fluttertoast.showToast( msg: 'Patient name: ${pat.name!.first.family}\nPatient last name: ${pat.name!.first.given!.first}', toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.black, textColor: Colors.white, fontSize: 16.0);*/ }, child: const Text('Get Patient'), ), ), //eventualmente se si volessero recuperare le observations legate a un paziente Center( child: ElevatedButton( onPressed: () async { var response = await http.get( Uri.parse('$server/Observation?subject=2936332'), headers: headers); Bundle bundle = Bundle.fromJson(jsonDecode(response.body)); String obsNumber = (bundle.entry != null && bundle.entry!.isNotEmpty) ? bundle.entry!.length.toString() : '0'; showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text( 'Observation related to Patient id: 2936332'), content: SingleChildScrollView( child: Text( obsNumber)), actions: [ TextButton( onPressed: () { Navigator.of(ctx).pop(); }, child: const Text("Ok"), ), ], ), ); //ci si vanno a gestire le entry!!!!! }, child: const Text('Get Patient\'s Observations'), ), ), ], ), ); } }