Flutter App在后台无法访问互联网



我正在构建一个出租车应用程序(在Android和IOS上运行),需要每隔10秒使用计时器更新驾驶员位置到数据库。
驱动程序当前位置正在被地理定位器包不断地挑选,然后发送到实时数据库。


更新只有在应用程序处于前台时才能正常工作
一旦应用程序被带到后台或最小化,我就会收到"没有互联网或wifi";错误,因此不更新位置。
Timer继续在后台运行,但由于没有互联网,它无法更新数据库。


我能做些什么来确保应用程序在后台接收互联网,以便它不断更新驱动程序位置?

计时器

Timer timer;
timer = Timer.periodic(Pallete.refreshTime, (timer) {
_getLocation();
});

_getLocation

Future<void> _getLocation() async {
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high).then((value) =>{
postLocation( context,'${value.latitude},${value.longitude}')
});
}
postLocation(BuildContext context,String location){
final myCubit = BlocProvider.of<DriveractivejobCubit>(context);
myCubit.updateLocation(context, location);
}

关于为什么会发生这种情况的更好的解释可以在这个链接中找到。
这也解释了我们必须实现这一点的几个选择。
感谢@dm_tr
stackoverflow.com/a/60441628/14231239

最新更新