// Needed import 'package:flutter/material.dart'; import 'dart:ui'; import 'dart:async'; import 'dart:convert'; // Local imports //import 'backgroundservices.dart' as background_services; import 'utils/storage.dart'; import 'utils/api.dart' as api; //import 'utils/time.dart'; // Background service import 'package:flutter_background_service/flutter_background_service.dart'; // Main app entry point Future main() async { WidgetsFlutterBinding.ensureInitialized(); // This is for testing only await appStorage.deleteAll(); await appStorage.write( key: 'server-url', value: 'http://192.168.68.103:3000', ); await appStorage.write(key: 'viewers', value: '{}'); debugPrint('viewers: ${await appStorage.read(key: "viewers")}'); final original = (await appStorage.read(key: 'viewers')).toString(); dynamic temp = jsonDecode(original); final json = '{"viewer_id": "viewernumxxx"}'; // This would be the response final viewerId = jsonDecode(json)['viewer_id']; temp[viewerId] = 'active'; final jsonNew = jsonEncode(temp); await appStorage.write(key: 'viewers', value: jsonNew); debugPrint('viewers: ${await appStorage.read(key: "viewers")}'); await startBackgroundLocationService(); startBackgroundService(); await api.sendPings(); runApp(const MainApp()); } class MainApp extends StatelessWidget { const MainApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold(body: Center(child: Text('Hello World!'))), ); } } // Background service Future startBackgroundLocationService() async { WidgetsFlutterBinding.ensureInitialized(); await initializeService(); } void startBackgroundService() { final service = FlutterBackgroundService(); service.startService(); } void stopBackgroundService() { final service = FlutterBackgroundService(); service.invoke("stop"); } Future initializeService() async { final service = FlutterBackgroundService(); await service.configure( iosConfiguration: IosConfiguration( autoStart: true, onForeground: onStart, onBackground: onIosBackground, ), androidConfiguration: AndroidConfiguration( autoStart: true, onStart: onStart, isForegroundMode: true, autoStartOnBoot: true, ), ); } @pragma('vm:entry-point') Future onIosBackground(ServiceInstance service) async { WidgetsFlutterBinding.ensureInitialized(); DartPluginRegistrant.ensureInitialized(); return true; } @pragma('vm:entry-point') void onStart(ServiceInstance service) async { Timer.periodic(const Duration(seconds: 1), (timer) async { /*await LocationUtils.updatePosition(); await api.sendLocation( LocationUtils.getLatitude(), LocationUtils.getLongitude(), );*/ }); } // Background service end