小组件状态在 iOS 上不会更新



我正在构建一个记录位置和加速度计数据的应用程序,并将该数据导出为CSV文件。

问题:

当我部署到android一切工作正常。
我有一个运行时钟,你可以看到在构建小部件:time:DateTime.now().toString()),.
位置更新和显示速度信息:speed: (_speed?.round() ?? 0),.

但是当我部署到ios时,小部件的状态根本没有更新。
时钟卡住,位置服务不更新。
如果我把特定的页面留给主页并再次打开它,时钟会更新到当前时间,但仍然卡住。
似乎由于某种原因,应用程序的状态在iOS中没有更新,我不明白
为什么。

我在info.plist中添加了以下行:

<key>NSMotionUsageDescription</key>
<string>banana</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>banana</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>banana</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>banana</string>

代码:

import 'package:flutter/material.dart';
import 'package:different/widgets/StatGridDrive.dart';
import 'package:location/location.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sensors/sensors.dart';
class DataStorage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/locationData.csv');
}
Future<File> writeData(String data) async {
final file = await _localFile;
// Write the file
return file.writeAsString(data, mode: FileMode.append);
}
}
class DriveScreen extends StatefulWidget {
final DataStorage data;
DriveScreen({Key key, @required this.data}) : super(key: key);
@override
_DriveScreenState createState() => _DriveScreenState();
}
class _DriveScreenState extends State<DriveScreen> {
double _speed;
Timer _timer;
String _accelerometerValues;
String _gyroscopeValues;
List<StreamSubscription<dynamic>> _streamSubscriptions =
<StreamSubscription<dynamic>>[];
@override
void initState() {
super.initState();
_streamSubscriptions
.add(accelerometerEvents.listen((AccelerometerEvent event) {
setState(() {
_accelerometerValues = event.x.toString() +
"," +
event.y.toString() +
"," +
event.z.toString();
});
}));
_streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) {
setState(() {
_gyroscopeValues = event.x.toString() +
"," +
event.y.toString() +
"," +
event.z.toString();
});
}));
}
//location data
final Location location = Location();
LocationData _location;
StreamSubscription<LocationData> _locationSubscription;
String _error;
Future<void> _listenLocation() async {
_locationSubscription =
location.onLocationChanged.handleError((dynamic err) {
setState(() {
_error = err.code;
});
_locationSubscription.cancel();
}).listen((LocationData currentLocation) {
setState(() {
_error = null;
_location = currentLocation;
_speed = _location.speed;
});
});
}
Future<void> _stopListen() async {
_locationSubscription.cancel();
}
Future<void> newData() async {
// Write the variable as a string to the file.
if (_location != null) {
widget.data.writeData(
'${_location.longitude.toString() + "," + _location.latitude.toString() + "," + _accelerometerValues + "," + _gyroscopeValues.toString()} n');
}
}
@override
void dispose() {
super.dispose();
for (StreamSubscription<dynamic> subscription in _streamSubscriptions) {
subscription.cancel();
_locationSubscription.cancel();
_timer.cancel();
}
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: GestureDetector(
onTap: () {
_stopListen();
Navigator.pop(context);
},
child: Icon(
Icons.baby_changing_station,
),
),
title: Text('LyftOff'),
backgroundColor: Color(0xFF282828)),
backgroundColor: Color(0xFF333333),
body: SizedBox(
height: 325,
child: StatsGrid(
distance: 15,
speed: (_speed?.round() ?? 0),
time: DateTime.now().toString()),
),
floatingActionButton: SizedBox(
height: 60.0,
width: 185.0,
child: FloatingActionButton.extended(
onPressed: () {
_listenLocation();
_timer = new Timer.periodic(
Duration(milliseconds: 100), (Timer t) => newData());
},
backgroundColor: Colors.green,
label: Text(
'התחל נסיעה',
style: TextStyle(
fontSize: 20.0,
),
),
icon: Icon(
Icons.car_rental,
size: 30.0,
),
splashColor: Colors.greenAccent,
),
),
));
}
}

任何指针我做错了什么?

在initstate()中使用setstate()查看更多信息:

[https://stackoverflow.com/questions/53363774/importance-of-calling-setstate-inside-initstate] [1]

最新更新