I have the below screen which contains user info and have a button Export CSV:
All I need when click Export CSV just export a file as the below format:
This is the CSV Controller file:
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;
}
}
}
and this is the below 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();
},
),
)),
),
);
}
}
So now I want to use the CSV controller file in the main class as to export the csv file which contains the user data.

