我尝试使用此代码加载和读取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.
答案 0 :(得分:0)
在pubspec.yaml
文件中添加完整路径:
flutter:
assets:
- assets/data/table.txt
您应该在主项目目录中创建一个assets
文件夹,其中存放诸如lib,build和IOS之类的文件夹,并将您的data
目录放入其中,然后添加上述行并运行命令flutter packages get
,并在完成后可以正常工作。