Dart-flutter的新手,我需要编写一个应用程序,每隔一小时就可以在后台选择用户位置。 我试图在地理定位插件和Timer类周围工作,它工作正常,但只选择一次位置 我需要知道是否有服务工作或最佳方式来解决这个问题 谢谢
class HomePage extends StatefulWidget{
static const timeout = const Duration(seconds: 5);
@override
State createState() => new HomePageState();
HomePage(){
startTimeout();
}
startTimeout()async {
final GeolocationResult result = await
Geolocation.isLocationOperational();
if(result.isSuccessful) {
return new Timer(timeout, handleTimeout);
}
else {
debugPrint(result.error.toString());
GeolocationResult res = await
Geolocation.requestLocationPermission(const LocationPermission(
android: LocationPermissionAndroid.fine,
ios: LocationPermissionIOS.always,
));
if(res.isSuccessful) {
new Timer.periodic(timeout,(Timer t)=> handleTimeout);
} else {
debugPrint(res.error.toString());
}
}
}
void handleTimeout(){
debugPrint("Print after 10 seconds");
Geolocation.currentLocation(accuracy:
LocationAccuracy.best).listen((result) {
if(result.isSuccessful) {
var posts = new postservice.Post();
var userid=100;
posts.sendLocation(result.location.latitude,result.location.longitude,userid);
}
});
}
}