0

我正在使用这个插件来更新我在 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);

                    });

            }
        }
4

1 回答 1

1

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

于 2021-09-01T14:02:53.540 回答