gpt4 book ai didi

flutter - 如何使用 Flutter 从 FaceBook 导出数据到 CSV 文件

转载 作者:行者123 更新时间:2023-12-04 15:01:33 31 4
gpt4 key购买 nike

我有下面的屏幕,其中包含用户信息并有一个按钮 Export CSV :
enter image description here
点击时我只需要Export CSV只需将文件导出为以下格式:
enter image description here
这是 CSV Controller 文件:

import 'package:csv/csv.dart';
import 'dart:io' as Io;
import 'package:path_provider/path_provider.dart';
import 'package:intl/intl.dart';
import 'package:simpleappauth/general.dart';

class CsvController {

static Future<Io.File> getCsvFromList(List<List<dynamic>> csvDataList) async {
try {
String csvDataString = const ListToCsvConverter().convert(csvDataList);
Io.File csvFile = await _saveFile(csvDataString);
return csvFile;
} catch (e) {
print(e.toString());
return null;
}
}

static Future<Io.File> getCsvFromString(String csvString) async {
try {
Io.File csvFile = await _saveFile(csvString);
return csvFile;
} catch (e) {
print(e.toString());
return null;
}
}

static Future<String> _getFilePath(String fileName) async {
Io.Directory appDocumentsDirectory = await getExternalStorageDirectory(); // 1
String appDocumentsPath = appDocumentsDirectory.path; // 2
String filePath = '$appDocumentsPath/$fileName.csv'; // 3
return filePath;
}

final DateFormat formatter = DateFormat('yyyy-MM-dd');

static Future<Io.File> _saveFile(String fileDataString, {index = 0}) async {
try {
Io.File file = Io.File(await _getFilePath(
"${DateTime.now().millisecondsSinceEpoch}" +
(index > 0 ? "($index)" : "")));
if (!file.existsSync()) {
// 1
file.writeAsStringSync(fileDataString); // 2
return file;
} else {
return _saveFile(fileDataString, index: index + 1);
}
} catch (e) {
print(e.toString());
return null;
}
}
}
这是下面的 main.dart :
import 'package:flutter/material.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as JSON;
import 'dart:io';

import 'package:simpleappauth/csv_controller.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}

class _MyAppState extends State<MyApp> {
bool _isLoggedIn = false;
Map userProfile;
final facebookLogin = FacebookLogin();


_loginWithFB() async {
final result = await facebookLogin.logIn(['email']);

switch (result.status) {
case FacebookLoginStatus.loggedIn:
final token = result.accessToken.token;
final graphResponse = await http.get(Uri.parse(
'https://graph.facebook.com/v10.0/me?fields=id,name,picture,email,name_format,birthday,hometown&access_token=${token}'));

final profile = JSON.jsonDecode(graphResponse.body);
print(profile);
setState(() {
userProfile = profile;
_isLoggedIn = true;
});
break;

case FacebookLoginStatus.cancelledByUser:
setState(() => _isLoggedIn = false);
break;
case FacebookLoginStatus.error:
setState(() => _isLoggedIn = false);
break;
}
}

_logout() {
facebookLogin.logOut();
setState(() {
_isLoggedIn = false;
});
}

@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: Center(
child: _isLoggedIn
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.network(
userProfile["picture"]["data"]["url"],
height: 100.0,
width: 100.0,
),
Text(userProfile["id"]),
Text(userProfile["name"]),
Text(userProfile["email"]),
Text(userProfile["name_format"]),
Text(userProfile["birthday"] ?? 'Birthday: empty'),
Text(userProfile["hometown"] ?? 'Hometown: empty'),
OutlinedButton(
child: Text("Logout"),
onPressed: () {
_logout();
},
),
OutlinedButton(
child: Text("Export CSV"),
onPressed: () {
},
),
],
)
: Center(
child: OutlinedButton(
child: Text("Login with Facebook"),
onPressed: () {
_loginWithFB();
},
),
)),
),
);
}
}
所以现在我想使用 main 中的 CSV Controller 文件类为导出 csv包含用户数据的文件。

最佳答案

第 1 步:只需在您的 Csvcontroller 类中添加这两个函数

 static List<List<dynamic>> getCsvListFromUserProfilesMap(
List<Map<String, dynamic>> userProfiles) {
List<List<dynamic>> csvDataRows = [];
List<dynamic> headerRow = ["id", "name", "email", "hometown"];
csvDataRows.add(headerRow);

userProfiles.forEach((userProfile) {
List<dynamic> dataRow = [
userProfile["id"],
userProfile["name"],
userProfile["email"],
userProfile["hometown"] ?? 'Hometown: empty'
];
csvDataRows.add(dataRow);
});
return csvDataRows;
}

static List<List<dynamic>> getCsvListFromUserProfileMap(
Map<String, dynamic> userProfile) {
List<List<dynamic>> csvDataRows = [];
List<dynamic> headerRow = ["id", "name", "email", "hometown"];
csvDataRows.add(headerRow);

List<dynamic> dataRow = [
userProfile["id"],
userProfile["name"],
userProfile["email"],
userProfile["hometown"] ?? 'Hometown: empty'
];
csvDataRows.add(dataRow);
return csvDataRows;
}


第 2 步:只需将以下代码添加到您的导出 CSV 按钮。
//don't forget to import the CsvController file in the main
For example or testing purpose,

//Initialize these variables in your code
var userProfile = [
{
"id": 123,
"name": "User 1",
"email": "user1@gmail.com",
"homeTown": "city1"
},
];

var userProfiles = [
{
"id": 123,
"name": "User 1",
"email": "user1@gmail.com",
"homeTown": "city1"
},
{
"id": 1234,
"name": "User 2",
"email": "user2@gmail.com",
"homeTown": "city2"
},
];


onPressed: () {

//if you just want to export only one profile
var userCsvData = CsvController.getCsvListFromUserProfileMap(userProfile);
var csvFile = await CsvController.getCsvFromList(userCsvData);
if(csvFile != null){
print("File created here :"+csvFile.path);
}else{
print("file not created");
}

//if you just want to export only multiple profiles
var userCsvData = CsvController.getCsvListFromUserProfilesMap(userProfiles);
var csvFile = await CsvController.getCsvFromList(userCsvData);
if(csvFile != null){
print("File created here :"+csvFile.path);
}else{
print("file not created");
}
},

关于flutter - 如何使用 Flutter 从 FaceBook 导出数据到 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66888497/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com