我一直在努力创建一个应用程序,即使应用程序被杀死,该应用程序也会将用户的位置数据发送到Firestore数据库.
I have been trying to create an app in flutter which sends the location data of a user to the Firestore Database EVEN WHEN THE APP IS KILLED.
当该应用被终止时,此插件可以很好地获取位置数据: github/rekab-app/background_locator
This plugin works well to get the location data when the app is killed: github/rekab-app/background_locator
问题是我无法在此插件的回调"功能中更新Firestore数据库值
The problem is that I am unable to update the Firestore Database values in the 'callback' function of this plugin
这是我的代码:
static void callback(LocationDto locationDto) async { print('location in dart: ${locationDto.toString()}'); final SendPort send = IsolateNameServer.lookupPortByName(_isolateName); send?.send(locationDto); await DatabaseService(uid:UID).updateUserLocation( (locationDto.latitude).toString(), (locationDto.longitude).toString()); }DatabaseService.updateUserLocation方法为:
The DatabaseService.updateUserLocation method is:
Future updateUserLocation(String latitude,String longitude) async { return await location_collection.document(uid).setData({ 'Longitude':longitude, 'Latitude':latitude }); }错误在于将locationDto发送到FireStore数据库 确切的错误是:
The error is in sending the locationDto to the FireStore Database The exact error is:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method DocumentReference#setData on channel plugins.flutter.io/cloud_firestore)这与updateUserLocation方法中的setData()函数有关.
This relates to the setData() function in the updateUserLocation method.
即使应用程序被杀死,如何将用户的位置数据发送到我的Firestore数据库中?
How do I send the location data of the user to my Firestore Database EVEN WHEN THE APP IS KILLED?
如果有人可以帮助我,那就太好了!!
It would be great if someone could help me on this!!!
谢谢!
推荐答案获取令牌时,我在shared_preferences中也遇到了同样的情况.因此,我认为在回调方法内创建一个新实例并起作用.我的意思是,尝试像新对象一样在回调方法中调用location_collection.
I had the same situation with shared_preferences, when get token. So, I think in create a new instance inside callback method, and works. I mean, try to call location_collection inside callback method like new object.
并将其添加到应用"应用中
And added this in Application app
override fun registerWith(registry: PluginRegistry?) { if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) { SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences")) } }更多推荐
当应用在FLUTTER中被杀死时,如何将位置数据发送到Firestore数据库
发布评论