I am attempting to create a document export custom action that uses this https://pub.dev/packages/docx_template package. It uses a word file template (stored in firebase) with tags that are replaced with the content in the code.
The code fails after the 3rd step. The error I am getting is:
"Error: Unsupported operation: Cannot modify an unmodifiable list".
I know this isn't a flutterflow issue per se, but does anyone know what I am doing wrong?
// 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!
// ... Other imports ...
import 'dart:io';
import 'dart:typed_data';
import 'package:docx_template/docx_template.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
Future<void> docBookDownload() async {
// Retrieve the template from Firebase Storage
final String templatePath =
'templates/template.docx'; // Update this path accordingly
final firebase_storage.Reference storageRef =
firebase_storage.FirebaseStorage.instance.ref(templatePath);
// Download the template file
final Uint8List? fileData = await storageRef.getData();
debugPrint("Step 1");
// Check if data is not null before proceeding
if (fileData != null) {
// Create a DocxTemplate from the downloaded data
final docx = await DocxTemplate.fromBytes(fileData);
debugPrint("Step 2");
// Create content for the template (replace with your actual content)
Content c = Content();
c
..add(TextContent("docname", "List of People"))
..add(TableContent("table", [
RowContent()
..add(TextContent("key1", "Bob"))
..add(TextContent("key2", "30")),
]));
debugPrint("Step 3");
// Generate the document
final d = await docx.generate(c);
debugPrint("Step 4");
// Save the generated document to a local file (assuming File is properly imported)
final of = File('generated.docx');
if (d != null) await of.writeAsBytes(d);
} else {
// Handle the case when the file data is null
print("Error: Failed to download the template file.");
}
}