Inconsistent CSV export rows

Custom Code

I am getting issues with my CSV export. Ever so often there will be a blank row or some of the data will end up in the wrong column. Anyone able to offer any suggestions on how to fix this / improve my code?

// 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 'package:download/download.dart';
import 'package:intl/intl.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<void> firestoreToCsvAndDownloadAction(DocumentReference docRef) async {
  // Access the subcollection 'FeedbackRecords' using the DocumentReference
  QuerySnapshot querySnapshot =
      await docRef.collection('FeedbackRecords').get();

  // Convert QuerySnapshot to List of Maps
  List<Map<String, dynamic>> firestoreList = querySnapshot.docs
      .map((DocumentSnapshot document) =>
          document.data() as Map<String, dynamic>)
      .toList();

  if (firestoreList.isEmpty) {
    print('Firestore collection is empty.');
    return;
  }
  List<String> headers = ['Timestamp', 'Postcode', 'Emotion', 'Comments'];

  // Create a string to hold the CSV data
  String csvData = headers.join(",") + "\n";

  // Loop through the objects and add their values to the CSV string
  for (Map<String, dynamic> firestoreData in firestoreList) {
    List<String> values = [];
    for (String header in headers) {
      if (header == 'Timestamp' && firestoreData[header] is Timestamp) {
        // Convert Timestamp to DateTime and format as a string or use an empty string if null
        final timestamp = firestoreData[header] as Timestamp?;
        final formattedDateTime = timestamp != null
            ? DateFormat('yyyy-MM-dd HH:mm:ss').format(timestamp.toDate())
            : '';
        values.add(formattedDateTime);
      } else {
        // Use the value as a string or use an empty string if null
        values.add(firestoreData[header]?.toString() ?? '');
      }
    }
    csvData += values.join(",") + "\n";
  }

  // Custom filename: CDSobsession_yyyyMMdd_HHmmss.csv
  final formattedDateTime =
      DateFormat('yyyyMMdd_HHmmss').format(DateTime.now());
  final fileName = 'CDSobsession_$formattedDateTime.csv';

  // Convert the CSV string to a list of bytes (Uint8List)
  Uint8List csvBytes = Uint8List.fromList(csvData.codeUnits);

  // Convert the Uint8List to a Stream<int>
  Stream<int> csvStream = Stream.fromIterable(csvBytes.map((byte) => byte));

  // Download the CSV file with the custom filename
  await download(csvStream, fileName);
}
What have you tried so far?

Dependencies set to -

csv: ^5.1.1

download: ^1.0.0

Did you check FlutterFlow's Documentation for this topic?
No
1 reply