Я использую пакет flutter_background_service вместе с Firebase для эта цель. Однако я столкнулся со следующей ошибкой:
Код: Выделить всё
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
Фрагмент кода:
Код: Выделить всё
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeService();
runApp(const MyApp());
}
Future initializeService() async {
await Firebase.initializeApp();
final service = FlutterBackgroundService();
await service.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: false,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
),
);
}
@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized();
await Firebase.initializeApp(); // Is this the correct way to initialize Firebase in the background?
Timer.periodic(const Duration(seconds: 10), (timer) async {
if (service is AndroidServiceInstance && await service.isForegroundService()) {
callBacFunction();
}
});
}
void callBacFunction() async {
final locationData = {
'latitude': Random().nextInt(100),
'longitude': Random().nextInt(100),
'timestamp': DateTime.now().toIso8601String(),
};
await FirebaseFirestore.instance
.collection('live_tracking')
.doc("1122")
.set(locationData, SetOptions(merge: true));
}
Является ли flutter_background_service лучшим вариантом для отслеживания в реальном времени или мне следует рассмотреть другие пакеты?
Как это сделать? Я уверен, что фоновая служба работает без проблем как на Android, так и на iOS?
Будем признательны за любые рекомендации и предложения. У меня нет опыта работы с фоновыми службами, поэтому подробные инструкции были бы полезны.
Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/792 ... in-flutter