在flutter中将位置数据导出为csv



我试图将位置数据导出为csv文件。
我试过使用flutter文档,但我不明白我做错了什么。

应用程序正在运行,我可以看到屏幕上的位置数据,
所以我认为问题是与将数据写入CSV文件。
我注意到的另一件事是,我的应用程序里面没有文件夹android/数据。

我请求许可的方式不对吗?
我在AndroidManifest.xml

中添加了以下行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

为了清楚起见,问题是没有创建文件。如果你能帮我,我很高兴。

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:permission_handler/permission_handler.dart';

class DataStorage {
Future<String> get _localPath async {
Permission.storage.request();
final directory = await getExternalStorageDirectory();
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);
}
}
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> {
@override
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;
});
});
}
Future<void> _stopListen() async {
_locationSubscription.cancel();
}
Future<File> newData() {
// Write the variable as a string to the file.
return widget.data.writeData(_location.longitude.toString() + "," + _location.latitude.toString());
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: GestureDetector(
onTap: () {
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: 100, time: (_error ?? '${_location ?? "0"}')),
),
floatingActionButton: SizedBox(
height: 60.0,
width: 185.0,
child: FloatingActionButton.extended(
onPressed: () {
_listenLocation();
},
backgroundColor: Colors.green,
label: Text(
'התחל נסיעה',
style: TextStyle(
fontSize: 20.0,
),
),
icon: Icon(
Icons.car_rental,
size: 30.0,
),
splashColor: Colors.greenAccent,
),
),
));
}
}

所以,我发现了问题。
和我想分享一些从这个过程中的见解。

见解:

1。如果您不需要,则无需询问权限,用户将有权访问数据。
所以我把getExternalStorageDirectory()换成了getApplicationDocumentsDirectory();

2。当您使用getApplicationDocumentsDirectory()时,数据将在
root/data/data/"app name"中。

3。在写入文件时,如果要追加新数据,则需要传递mode:FileMode.append作为参数,否则您将在旧行上写入。
file.writeAsString(data, mode:FileMode.append);

代码不能工作的主要原因:

  1. 当我从main调用当前页面时,我没有初始化数据存储类。
    主代码:
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => new DriveScreen(data: DataStorage())),
);
},

你需要调用data: "your_class_name"

  1. 我没有调用写入函数newData().
    它被添加在这里:
Future<void> _listenLocation() async {
_locationSubscription =
location.onLocationChanged.handleError((dynamic err) {
setState(() {
_error = err.code;
});
_locationSubscription.cancel();
}).listen((LocationData currentLocation) {
setState(() {
_error = null;
_location = currentLocation;
});
newData();
});
}

工作代码:

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';

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> {
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;
});
newData();
});
}
Future<void> _stopListen() async {
_locationSubscription.cancel();
}
Future<File> newData() {
// Write the variable as a string to the file.
return widget.data.writeData('${_location.longitude.toString() + "," + _location.latitude.toString()} n');
}
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: 100, time: (_error ?? '${_location ?? "0"}')),
),
floatingActionButton: SizedBox(
height: 60.0,
width: 185.0,
child: FloatingActionButton.extended(
onPressed: () {
_listenLocation();
},
backgroundColor: Colors.green,
label: Text(
'התחל נסיעה',
style: TextStyle(
fontSize: 20.0,
),
),
icon: Icon(
Icons.car_rental,
size: 30.0,
),
splashColor: Colors.greenAccent,
),
),
));
}
}

最新更新