我如何使用GeoFire插件在后台更新我的位置



我使用这个插件来更新我在firebase中的位置。当应用程序在前台,一切工作完美,但一旦我的应用程序进入后台,然后位置更新服务停止,我尝试使用didChangeAppLifecycleState,但我似乎不能让它工作,这是我目前为止的实现。

class HomeTabPage extends StatefulWidget {

@override
_HomeTabPageState createState() => _HomeTabPageState();
}
class _HomeTabPageState extends State < HomeTabPage >
with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.detached) return;
final isBackground = state == AppLifecycleState.paused;
if (isBackground) {
print("### paused");
StreamSubscription < Position > backgroundStreamSubscription =
Geolocator.getPositionStream().listen((Position position) {
initPositionPoint = position;
Geofire.setLocation(
currentFirebaseUser.uid, position.latitude, position.longitude);
});
}
}

来自Github问题#53和#444的flutter-geolocator repo,它似乎不支持后台位置跟踪。似乎有些人一直在使用background_locator作为替代,所以你可能想看看。

最新更新