I have a local asset, a Docx file, (or in some cases only the UInt8List bytes), how is it possible to read the data from this file (or from bytes)?
This data contains only strings, how could I read it? I can read .txt file, but no docx, why? In fact there is no special character in it, so the problem is not with the coding. (Check content below, HtmlCode)
I tried: This is for the bytes
final data = await rootBundle.load('template.docx');
final bytes = data.buffer.asUint8List();
and This is (would be) for the string
String fileText = await rootBundle.loadString('template.docx');
Error:
Error: FormatException: Unexpected extension byte (at offset 16)
at Object.throw_ [as throw] (http://localhost:54436/dart_sdk.js:5366:11)
at convert._Utf8Decoder.new.convertGeneral (http://localhost:54436/dart_sdk.js:49632:19)
at convert._Utf8Decoder.new.convertSingle (http://localhost:54436/dart_sdk.js:49604:19)
at Utf8Decoder.convert (http://localhost:54436/dart_sdk.js:49463:67)
at Utf8Codec.decode (http://localhost:54436/dart_sdk.js:49172:22)
at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:54436/packages/flutter/src/services/system_channels.dart.lib.js:2289:31)
at loadString.next (<anonymous>)
at http://localhost:54436/dart_sdk.js:39272:33
at _RootZone.runUnary (http://localhost:54436/dart_sdk.js:39129:58)
at _FutureListener.thenAwait.handleValue (http://localhost:54436/dart_sdk.js:34091:29)
at handleValueCallback (http://localhost:54436/dart_sdk.js:34651:49)
at Function._propagateToListeners (http://localhost:54436/dart_sdk.js:34689:17)
at _Future.new.[_completeWithValue] (http://localhost:54436/dart_sdk.js:34531:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:54436/dart_sdk.js:34554:35)
at Object._microtaskLoop (http://localhost:54436/dart_sdk.js:39416:13)
at _startMicrotaskLoop (http://localhost:54436/dart_sdk.js:39422:13)
at http://localhost:54436/dart_sdk.js:34905:9
There is no problem at all with the bytes, but with the string, this error will be thrown. How could I solve this?
Full code:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Html(),
);
}
}
class Html extends StatefulWidget {
LoadHtml createState() => LoadHtml();
}
class LoadHtml extends State<Html> {
void initState() {
readFilesFromAssets();
super.initState();
}
// Read .docx function not only .docx mostly any type .json .txt etc
readFilesFromAssets() async {
print("read now");
String assetContent = await rootBundle.loadString('assets/template.docx');
print("assetContent : $assetContent");
}
//this is exaclty what would be in the template.doc
var HtmlCode = r"""
<HTML>
<BODY LANG="ru-RU" DIR="LTR">
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Example
Document</SPAN></FONT></FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"> <FONT FACE="Times New Roman, serif"><FONT SIZE=5><SPAN LANG="en-US"><B>Document
name</B></SPAN></FONT></FONT></P>
<P LANG="en-US" CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in">
<BR>
</P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Szerzodes
tobbi resze:</SPAN></FONT></FONT></P>
</BODY>
</HTML>
""";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('From docx')),
body: WebViewX(
initialContent: HtmlCode, //I should read this from the .docx
initialSourceType: SourceType.HTML,
),
);
}
}

You can use the Docs to text packages
Readme Changelog Example Installing Versions Scores This is a small package that allows you to retrieve text (text only) from .docx files (Microsoft Word).
Thanks SnowLukin
Usage
import 'package:docx_to_text/docx_to_text.dart';
final file = File('path/to/file.docx');
final bytes = await file.readAsBytes();
final text = docxToText(bytes);
First add your .docx file inside the assets folder as same level with lib folder.

Then add your assets folder to pubspec.yaml like this
assets:
- assets/
After than inside your main.dart add this function i.e readFileFromAssets() inside initState() to it will start loading data from .docx when app starts.
And import this package too import 'package:flutter/services.dart';
void initState() {
readFilesFromAssets();
super.initState();
}
// Read .docx function not only .docx mostly any type .json .txt etc
readFilesFromAssets() async {
String assetContent = await rootBundle.loadString('assets/mytext.docx');
print("assetContent : $assetContent");
}
Here is the output:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With