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.