[英]How to send user location to Firestore even when app is killed in flutter?
[英]How to send location data to Firestore Database when app is killed in FLUTTER
我一直在嘗試在 flutter 中創建一個應用程序,即使應用程序被殺死,它也會將用戶的位置數據發送到 Firestore 數據庫。
這個插件可以很好地在應用程序被殺死時獲取位置數據: https://github.com/rekab-app/background_locator
問題是我無法更新此插件的“回調”function 中的 Firestore 數據庫值
這是我的代碼:
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 方法是:
Future updateUserLocation(String latitude,String longitude) async
{
return await location_collection.document(uid).setData({
'Longitude':longitude,
'Latitude':latitude
});
}
錯誤在於將 locationDto 發送到 FireStore 數據庫 確切的錯誤是:
[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() function 有關。
即使應用程序被殺死,如何將用戶的位置數據發送到我的 Firestore 數據庫?
如果有人可以幫助我,那就太好了!!!
謝謝!
獲取令牌時,我與 shared_preferences 有相同的情況。 所以,我認為在回調方法中創建一個新實例,並且可以工作。 我的意思是,嘗試在新的 object 等回調方法中調用 location_collection。
並在應用程序應用程序中添加了這個
override fun registerWith(registry: PluginRegistry?) {
if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) {
SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences"))
}
}
要使用 Firestore,您應該將這些行添加到您的 Application.kt。
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.pathprovider.PathProviderPlugin
// Add this line
import io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin;
import io.flutter.view.FlutterMain
import rekab.app.background_locator.LocatorService
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
LocatorService.setPluginRegistrant(this)
FlutterMain.startInitialization(this)
}
override fun registerWith(registry: PluginRegistry?) {
if (!registry!!.hasPlugin("io.flutter.plugins.pathprovider")) {
PathProviderPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.pathprovider"))
}
// Add this section
if (!registry!!.hasPlugin("io.flutter.plugins.firebase.firestore")) {
CloudFirestorePlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebase.firestore"))
}
}
}
它對我有用。 讓我知道它是否也適合你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.