Usage of docx_template

My UseCase - I have a string which i want to add to an empty word document. Basically, I want to create a new word docx and add that string to it and be able to share it as well.

Code I used -
**static Future createDocx(String docText) async {
print(docText);

final data = await rootBundle.load('assets/docFile/docfile.docx');
final bytes = data.buffer.asUint8List();

var docx = await DocxTemplate.fromBytes(bytes);
print(docx);
Content c = Content();
c
  ..add(TextContent("content", "hi my name is yashi"))
  ..add(TextContent('name', 'yashi'));

print('Content object: $c');

// Generate the document with replaced variables
try {
  print('hiii');
  final docGenerated =
      await docx.generate(c, tagPolicy: TagPolicy.saveText);
  print('hi2');
  final directory = await getApplicationDocumentsDirectory();
  final filePath = '${directory.path}/output.docx';
  print(filePath);
  final file = File(filePath);
  if (docGenerated != null) await file.writeAsBytes(docGenerated);
  await Share.shareXFiles([XFile(filePath)]);
  print('Document created: $filePath');
} catch (e) {
  print('Error generating document: $e');
}

}**

Console Output -
I/flutter (14867): Instance of ‘DocxTemplate’
I/flutter (14867): Content object: {content: {}, name: {}}
I/flutter (14867): hiii
I/flutter (14867): Error generating document: Unsupported operation: Cannot modify an unmodifiable list

(It also prints docText correctly)

This is how my word template looks like-

Pls suggest me what’s wrong.

Some other ways to achieve the desired use case are also welcomed.

Thanks.

From the code here I don’t see any error. Best to enable “break on all exceptions” in the debug settings so you really see where the problem is or add a second parameter “stackTrace” to your catch and print that too and share it here

Hey, how do we print stacktrace.
Could you please share that

Catch can be followed by (var) or (var, var) and the second variable will be a stacktrace.For example

catch (e, stacktrace) {
  print('Error generating document: $e');
}
1 Like

and make sure you have enabled this checkboxes

still, not able to resolve the issue.

I/flutter (12127): StackTrace: #0 UnmodifiableListMixin.= (dart:_internal/list.dart:90:5)
I/flutter (12127): #1 DocxEntry._updateData (package:docx_template/src/docx_entry.dart:29:17)
I/flutter (12127): #2 DocxXmlEntry._updateArchive (package:docx_template/src/docx_entry.dart:58:7)
I/flutter (12127): #3 DocxManager.updateArch. (package:docx_template/src/docx_entry.dart:203:13)
I/flutter (12127): #4 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)
I/flutter (12127): #5 DocxManager.updateArch (package:docx_template/src/docx_entry.dart:202:10)
I/flutter (12127): #6 DocxTemplate.generate (package:docx_template/src/template.dart:96:14)
I/flutter (12127): #7 FileUtils.createDocx (package:law_diary/widgets/fileFunctions.dart:175:39)
I/flutter (12127):
I/flutter (12127): #8 _DocxViewerState.build. (package:law_diary/miscFiles/editFile.dart:103:15)
I/flutter (12127):

this is the stackTrace