Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振中如何从docx文件中读取数据_Flutter_File_Dart_Docx_Readfile - Fatal编程技术网

Flutter 颤振中如何从docx文件中读取数据

Flutter 颤振中如何从docx文件中读取数据,flutter,file,dart,docx,readfile,Flutter,File,Dart,Docx,Readfile,我有一个本地资产,一个Docx文件(或者在某些情况下只有UInt8List字节),如何从该文件(或字节)读取数据 此数据仅包含字符串,如何读取?我可以读取.txt文件,但没有docx,为什么? 事实上,它没有特殊字符,所以问题不在于编码。(检查以下内容,HtmlCode) 我试过: 这是用于字节的 final data = await rootBundle.load('template.docx'); final bytes = data.buffer.asUint8List(); 及

我有一个本地资产,一个Docx文件(或者在某些情况下只有UInt8List字节),如何从该文件(或字节)读取数据

此数据仅包含字符串,如何读取?我可以读取.txt文件,但没有docx,为什么? 事实上,它没有特殊字符,所以问题不在于编码。(检查以下内容,HtmlCode)

我试过: 这是用于字节的

final data = await rootBundle.load('template.docx');
    final bytes = data.buffer.asUint8List();
及 这是(将是)字符串

String fileText = await rootBundle.loadString('template.docx');
错误:

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
错误:FormatException:意外的扩展字节(在偏移量16处)
at Object.throw uu[as throw](http://localhost:54436/dart_sdk.js:5366:11)
转换时。\u Utf8Decoder.new.convertGeneral(http://localhost:54436/dart_sdk.js:49632:19)
转换时。\u Utf8Decoder.new.convertSingle(http://localhost:54436/dart_sdk.js:49604:19)
在Utf8Decoder.convert(http://localhost:54436/dart_sdk.js:49463:67)
在Utf8Codec.decode(http://localhost:54436/dart_sdk.js:49172:22)
在asset_bundle.PlatformAssetBundle.new.loadString(http://localhost:54436/packages/flutter/src/services/system_channels.dart.lib.js:2289:31)

在loadString.next(

首先将您的
.docx
文件添加到
资产
文件夹中,与
文件夹的级别相同。

然后像这样将您的
资产
文件夹添加到
pubspec.yaml

  assets:
    - assets/
之后,在
main.dart中添加此函数,即
readfilefromsets()
inside
initState()
将在应用程序启动时开始从
.docx
加载数据

然后也导入这个包
import'package:flatter/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");
}
以下是输出:

您是否在assets中添加了
.docx
文件,并在
pubspec.yaml
中添加了文件路径,如
assets:–assets/myfile.docx
?是的,我只是尝试复制您的代码1:1以查看其是否有效,但没有,请在下面的注释中检查我的答案,我不明白为什么使用.txt它会像一个符咒一样有效,但是t docx不存在。WebViewX是否在webview\u Flatter软件包中?我不知道,但您的代码运行良好,我复制了它,并且运行平稳。确定。删除您的.docx文件并创建一个新文件。不要通过打开word来添加内容,而是不要打开MS word来添加内容。使用记事本或升华文本在y中添加内容我们的文件和复制粘贴您的HTML代码。可以这样做。但是它的加载数据没有显示在
WebViewX
上,谢谢您的详细回答。是的,我也这样做。我的docx中有资产文件夹,pubspec.yaml也在那里,但仍然:下面是我的错误:``[错误:flatter/lib/ui/ui\u dart\u state.cc(213)]未处理的异常:FormatException:意外扩展字节(偏移量16处)#0#Utf8Decoder.convertSingle(dart:convert patch/convert#patch.dart:1788:7)#1 Utf8Decoder.convert(dart:convert/utf.dart:318:42)E/Flatter(17417):#2 Utf8Codec.decode(dart:convert/utf.dart:63:20)E/Flatter(17417):#3 AssetBundle.loadString(package:flatter/src/services/asset_bundle.dart:74:19)E/flatter(17417):E/flatter(17417):#4 LoadHtml.readFilesFromAssets(package:docxflutter/main.dart:373:27)``为您的问题添加完整代码。更新了问题,感谢您指出这一点
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");
}