我正在构建一个记录位置和加速度计数据并将该数据导出到 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,
),
),
));
}
}
是否有任何指示我做错了什么?
答案 0 :(得分:0)
您正在 initstate() 中使用 setstate(),请查看更多信息:
[https://stackoverflow.com/questions/53363774/importance-of-calling-setstate-inside-initstate][1]