How to send location data to Firestore Database when app is killed in FLUTTER

丶灬走出姿态 提交于 2020-05-28 08:03:05

问题


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.

This plugin works well to get the location data when the app is killed: https://github.com/rekab-app/background_locator

The problem is that I am unable to update the Firestore Database values in the 'callback' function of this plugin

Here is my code:

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());

  } 

The DatabaseService.updateUserLocation method is:

 Future updateUserLocation(String latitude,String longitude) async
  {
    return await location_collection.document(uid).setData({
      'Longitude':longitude,
      'Latitude':latitude
    });
  }

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)

This relates to the setData() function in the updateUserLocation method.

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!!!

Thanks!


回答1:


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"))
        }
    }


来源:https://stackoverflow.com/questions/61321708/how-to-send-location-data-to-firestore-database-when-app-is-killed-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!