gpt4 book ai didi

firebase - 即使应用程序在 flutter 中被杀死,如何将用户位置发送到 Firestore?

转载 作者:行者123 更新时间:2023-12-05 05:58:39 25 4
gpt4 key购买 nike

我正在尝试将用户的位置发送到 Firestore,即使该应用已被终止。当应用程序处于前台或后台时,我能够成功地将数据发送到 Firestore,但当应用程序被终止时,我无法发送数据。任何人都可以解释可能是什么问题吗?

我正在使用

background_locator: ^1.6.4

flutter

Flutter 2.2.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f4abaa0735 (2 weeks ago) • 2021-07-01 12:46:11 -0700
Engine • revision 241c87ad80
Tools • Dart 2.13.4

Dart

Dart SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "linux_x64"

接收到数据后我尝试执行的这段代码

receiverPort.listen(
(dynamic data) async {
if (data != null) {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
final LocationDto position = data as LocationDto;
print('data: $position');

final Member member = await FirebaseService().getCurrentUserProfile();
member.currentPosition = '${position.latitude},${position.longitude}';
await FirebaseService()
.updateData(
FirebaseService.memberRef,
FirebaseService.memberChildId,
FirebaseService.getCurrentUserId(),
member.toJson())
.then((value) {});
} else {
print('data is null');
}
},
);

如果我需要分享任何其他内容,请告诉我。谢谢

最佳答案

我发现了问题。我需要在回调中而不是在 ReceivePort 中添加 Firestore 代码。这是工作代码:

Future<void> _startLocator() async {
final Map<String, dynamic> data = {'countInit': 1};
return BackgroundLocator.registerLocationUpdate(
LocationCallbackHandler.callback, // <-- You need to add your callback code here
initCallback: LocationCallbackHandler.initCallback,
initDataCallback: data,
disposeCallback: LocationCallbackHandler.disposeCallback,
iosSettings: const IOSSettings(
accuracy: location_settings.LocationAccuracy.BALANCED,),
androidSettings: AndroidSettings(
accuracy: location_settings.LocationAccuracy.BALANCED,
interval: 5,
androidNotificationSettings: AndroidNotificationSettings(
notificationChannelName: 'Location tracking',
notificationTitle:
location.isNotEmpty ? location : 'Start Location Tracking',
notificationMsg: 'Track location in background',
notificationBigMsg:
'Background location is on to keep the app up-to-date with your location. This is required for main features to work properly when the app is not running.',
notificationIconColor: Colors.grey,
notificationTapCallback:
LocationCallbackHandler.notificationCallback)));
}
Future<void> callback(LocationDto locationDto) async {
// Your call back code goes here
}

关于firebase - 即使应用程序在 flutter 中被杀死,如何将用户位置发送到 Firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68417562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com