gpt4 book ai didi

dart - 如何在 flutter 中读取 txt 文件?

转载 作者:IT王子 更新时间:2023-10-29 07:04:29 24 4
gpt4 key购买 nike

我试过这段代码来加载和读取一个 txt 文件。但它无法读取文件并返回“无法读取文件”消息。

Future<String> _read() async {
String text;
try {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/table.txt');
text = await file.readAsString();
} catch (e) {
print("Couldn't read file");
}
return text;
}

我将我的 txt 文件设置在项目目录中的文件夹(数据)中。我用 final file = File('${directory.path}/data/table.txt'); 也是。这是我的“pubspec.yaml”文件:

flutter:
assets:
- data/table.txt

我收到这个错误:

I/flutter ( 2067): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 2067): The following assertion was thrown building MyApp(dirty):
I/flutter ( 2067): 'package:flutter/src/widgets/text.dart': Failed assertion: line 235 pos 15: 'data != null': is not
I/flutter ( 2067): true.
I/flutter ( 2067):
I/flutter ( 2067): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 2067): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 2067): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 2067): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 2067):
I/flutter ( 2067): When the exception was thrown, this was the stack:
I/flutter ( 2067): #2 new Text
package:flutter/…/widgets/text.dart:235
I/flutter ( 2067): #3 MyApp.build
package:hello_flutter/main.dart:90
I/flutter ( 2067): #4 StatelessElement.build
package:flutter/…/widgets/framework.dart:3774
I/flutter ( 2067): #5 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3721
I/flutter ( 2067): #6 Element.rebuild
package:flutter/…/widgets/framework.dart:3547
I/flutter ( 2067): #7 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2286
I/flutter ( 2067): #8 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:676
I/flutter ( 2067): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:219
I/flutter ( 2067): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:990
I/flutter ( 2067): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:930
I/flutter ( 2067): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure>
package:flutter/…/scheduler/binding.dart:751
I/flutter ( 2067): #14 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter ( 2067): #15 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter ( 2067): #16 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_pat
ch.dart:171:12)
I/flutter ( 2067): (elided 3 frames from class _AssertionError and package dart:async)
I/flutter ( 2067): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 2067): Couldn't read file
I/flutter ( 2067): null
Reloaded 1 of 420 libraries in ۱٬۱۰۴ms.

最佳答案

pubspec.yaml 文件中添加完整路径:

flutter:
assets:
- assets/data/table.txt

您应该在主项目目录中创建一个 assets 文件夹,其中包含 lib、build 和 IOS 等文件夹,并将您的 data 目录放入其中,然后添加上述行并运行命令 flutter packages get 完成后它将正常工作。

关于dart - 如何在 flutter 中读取 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com