arbiter
arbiter

Reputation: 451

Exporting ListView Items to Text File in Flutter

I have this list view with list tile as a child. It shows a list of emails dynamically inputted by user. Is it possible to have one click export somewhere locally into a text file or even better an Excel file, just something convenient for main user to be able to see.

Here's my code, the RaisedButton onTap is null for now, but I would like it to have a function to export all the contents of ListView into a single file saved locally on the device.

body: Container(
    child: BlocConsumer<MailBloc, List<Email>>(
      builder: (context, mailList) {
        return ListView.separated(
          itemCount: mailList.length,
          itemBuilder: (BuildContext context, int index) {
            Email mail = mailList[index];
            return Card(
              child: ListTile(
                trailing: Icon(Icons.delete),
                onTap: () =>
                    DatabaseProvider.db.deleteMail(mail.id).then((_) {
                  BlocProvider.of<MailBloc>(context).add(
                    DeleteMail(index),
                  );
                }),
                title: Text(
                  mail.email,
                  style: TextStyle(fontSize: 20.0),
                ),
              ),
            );
          },
          separatorBuilder: (BuildContext context, int index) =>
              Divider(color: Colors.black),
        );
      },
      listener: (BuildContext context, mailList) {},
    ),
  ),
  floatingActionButton: FloatingActionButton(
    child: Text('Export'),
    onPressed: null,
  ),

**Since there probably isn't a one cut solution for exporting to excel, is there a way to one click copy (as in copy to clipboard) all ListView items? I did it by copying one with the flutter clipboard addon, but is there a function that would copy all of them? **

Upvotes: 2

Views: 1394

Answers (1)

Ric17101
Ric17101

Reputation: 1388

I think the easiest way to do this, imo, is to use https://pub.dev/packages/csv, since CSV file can also be imported to an excel application :), this works for me though and depending on your use case.

For file and permission handling i just use https://pub.dev/packages/simple_permissions for simple demonstration how you'll gonna save file csv as well as https://pub.dev/packages/path_provider to support filesystem locations on all platforms.

getCsv(List<Email> emailList) async {
  List<List<dynamic>> rows = List<List<dynamic>>();
  for (int i = 0; i < emailList.length; i++) {
    List<dynamic> row = List();
    row.add(emailList[i].id);
    row.add(emailList[i].email);
    rows.add(row);
  }

  await SimplePermissions.requestPermission(Permission.WriteExternalStorage);
  bool checkPermission = await SimplePermissions.checkPermission(Permission.WriteExternalStorage);
  if (checkPermission) {
    String dir = (await getExternalStorageDirectory()).absolute.path + "/documents";
    String file = "$dir";
    File f = new File(file + "filename.csv");
    String csv = const ListToCsvConverter().convert(rows);
    f.writeAsString(csv);
  }
}

pubspec.yaml

dependencies:
    flutter:
      sdk: flutter
  
    cupertino_icons: ^1.0.0
    csv: ^4.1.0
    simple_permissions: ^0.1.9
    path_provider: ^1.6.24

Upvotes: 1

Related Questions