HospifyDashboard
Docs menu

Flutter

hospify_org_sdk for Android and iOS. It uses geolocator for location and asks for permission the first time it's needed.

Terminal
flutter pub add hospify_org_sdk

Start it

Dart
import 'package:hospify_org_sdk/hospify_org_sdk.dart';

final hospify = Hospify.start(
  publishableKey: const String.fromEnvironment('HOSPIFY_KEY'),   // hpk_test_... / hpk_live_...
  getToken: () async => (await api.post('/hospify/token')).data['token'] as String,
  location: GeolocatorLocation(battery: () async => await Battery().batteryLevel),   // battery_plus, optional
);

The SOS button

Dart
class SosButton extends StatefulWidget {
  const SosButton({super.key});
  @override
  State<SosButton> createState() => _SosButtonState();
}

class _SosButtonState extends State<SosButton> {
  SosState? sos;
  late final StreamSubscription<SosState> sub;

  @override
  void initState() {
    super.initState();
    sub = hospify.sos.updates.listen((s) => setState(() => sos = s));
    hospify.sos.resume();   // an SOS left open when the app was closed
  }

  @override
  void dispose() {
    sub.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (sos?.isOpen ?? false) {
      return Column(children: [
        Text(sos!.responder != null ? '${sos!.responder} is on it' : 'Alert sent. Help is being found.'),
        FilledButton(onPressed: () => hospify.sos.safe(), child: const Text("I'm safe")),
        TextButton(onPressed: () => hospify.sos.cancel(), child: const Text('It was a mistake')),
      ]);
    }
    return GestureDetector(
      onLongPress: () async {
        try {
          final s = await hospify.sos.start(note: 'Need help');
          setState(() => sos = s);
        } on HospifyError catch (e) {
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.message)));
        }
      },
      child: const CircleAvatar(radius: 60, child: Text('Hold for SOS')),
    );
  }
}

Background location

Follow geolocator's setup: on Android, ACCESS_FINE_LOCATION, FOREGROUND_SERVICE_LOCATION and a foreground service; on iOS, NSLocationWhenInUseUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription and the location background mode.