Download your firebase in a csv file

#FidelitasUniversity

I created this custom code.

i made this custom code which works to download csv file from database

first create a button and bind it with a custom action that calls: downloadCollectionCSV
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:convert' show utf8;
import 'package:download/download.dart';

Future downloadCollectionCSV(List<ProductosRecord>? docs) async {
  docs = docs ?? [];
  String fileContent = "Nombre, Precio";

  docs.asMap().forEach((index, record) => fileContent = fileContent +
      "\n" +
      record.nombre.toString() +
      "," +
      record.precio.toString());

  final fileName = "FF" + DateTime.now().toString() + ".csv";
  var bytes = utf8.encode(fileContent);

  final stream = Stream.fromIterable(bytes);
  return download(stream, fileName);
}

// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the button on the right!

This is how my database looks, to reuse the code you should only customize the data you want to see in csv

Now when you press the button to which the custom action was linked, the csv file of the data you chose from your database should automatically be downloaded.

4
4 replies