flutter - 小部件状态不会在 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]
推荐阅读
- jquery - 如何使用 replace() 在 Jquery 之前删除逗号和字符串?
- react-native - 使用 iOS Accessibility Inspector 检索到的 ReactNative 的 `accessibilityLabel` 和 iOS Label 是否相同?
- javascript - Javascript 库不适用于 React Native 发布版本
- postgresql - 如何选择不符合排除约束的行
- selenium - 有人可以建议我,如何为图像编写 xpath。请在下面找到 xpath 是否有效
- crystal-reports - Crystal Reports:在图表上突出显示最小值和最大值
- python - 无法运行python单元测试
- reactjs - 只想从文件对话框中选择目录路径
- windows - 巨大的内存分配导致 Windows 完全无响应
- angular - 点击时隐藏/显示建议