PavelS0 / docx_template_dart

A Docx template engine
Apache License 2.0
40 stars 20 forks source link

Unhandled Exception: type 'TextContent' is not a subtype of type 'PlainContent?' of 'c' #20

Closed sputtw closed 1 year ago

sputtw commented 2 years ago

Hi, i get this error : E/flutter (16144): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'TextContent' is not a subtype of type 'PlainContent?' of 'c'

but i read that you said youu fixed it in commit a864546 but it still don't work for me

code.. throws this error when i do "await docx.generate(c);"

  ..add(ListContent("multilineList", [
    PlainContent("multilinePlain")
      ..add(TextContent('multilineText', 'line 1')),
    PlainContent("multilinePlain")
      ..add(TextContent('multilineText', 'line 2')),
    PlainContent("multilinePlain")
      ..add(TextContent('multilineText', 'line 3'))
  ]))
  ..add(TextContent('multilineText2', 'line 1\nline 2\n line 3'))
  ..add(ImageContent('img', testFileContent));
final d = await docx.generate(c);
aidingh commented 2 years ago

I have the same issue, any fix to this?

PavelS0 commented 2 years ago

what version of the dart sdk and docx_template are you using?

aidingh commented 2 years ago

Dart SDK version: 2.13.3 (stable) and docx_template version: docx_template: ^0.3.1

PavelS0 commented 2 years ago

in the example, there was data that did not match the template, I updated it, now it should work. Try to use new example.

You can delete the image loading lines for the test: final testFileContent = await File('test.jpg').readAsBytes();

..add(ImageContent('img', testFileContent));

aidingh commented 2 years ago

Thank you Pavel for such a fast reply! I just tested it out and it worked like a charm. Thank you! Now I just gotta set up my own template. Do you think i can do it with Libre Office Writer?

PavelS0 commented 2 years ago

I've tried LibreOffice Writer, but it doesn't support content controls tags in the way that MS Word supports.

I am currently using MS Word 2007 via PlayOnLinux.

aidingh commented 2 years ago

Well iam almost done with my template and did use MS word on my windows computer to do the tags. Everything works great! Only thing i have been stuck on today is to add table rows in a dynamic way. This code only takes the last image from the list and adds it to the document . If i do it manually like the example you provided it works fine. Any tips on how i can add rows in a template in a dynamic way?

for(var val in imageMap.values){
  if(val.image != null){
    c.add(TableContent("table_img", [
      RowContent()
        ..add(TextContent("key1", val.imageText))
        ..add(ImageContent('img', await val.image.readAsBytes())),
    ]));
  }
}