作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读 https://flutter.dev/docs/development/ui/assets-and-images#asset-images-in-package-dependencies和 Flutter - Read text file from assets并应用了所有的东西,但我的代码仍然不起作用....
我为此打开了一个新项目,在主文件夹中我创建了 Assets 和一个文件:
pwd
/home/bboett/AndroidStudioProjects/examen_companion
bboett@hayate:~/AndroidStudioProjects/examen_companion$ ls -l assets/
insgesamt 4
-rw-r--r-- 1 bboett bboett 10 19. Mai 15:14 test.txt
bboett@hayate:~/AndroidStudioProjects/examen_companion$ cat assets/test.txt
Hello!!
然后,不信任 android studio,我检查了 vi,在 pubspec.yaml 中一切正常:
flutter:
uses-material-design: true
assets:
- assets/
我替换了空间,在使用和 Assets 之前有 2 个空格:和 4 之前 - Assets ...
@override
Widget build(BuildContext context)
{
AssetBundle bundle = DefaultAssetBundle.of(context);
return FutureBuilder<String>(
future: bundle.loadString("assets/test.txt"),
builder: (context, AsyncSnapshot<String> snapshot)
{
if (snapshot.hasData) { return Text(snapshot.data.toString()); }
else { return CircularProgressIndicator(); }
}
);
}
不起作用....我从不退出进度指示器....
void main() async {
print(await rootBundle.loadString("assets/test.txt"));
runApp(MyApp());
}
并且崩溃与:
Launching lib/main.dart on Linux in debug mode...
Building Linux application...
Debug service listening on ws://127.0.0.1:41355/L-ev6_eNIlI=/ws
Syncing files to device Linux...
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:222:39)
#1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:68:33)
#2 CachingAssetBundle.loadString.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:165:56)
#3 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:311:23)
#4 CachingAssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:165:27)
#5 main (package:examen_companion/main.dart:9:26)
我在运行之前改变了任何东西之后做了一个 flutter 的清洁......
flutter upgrade
Flutter is already up to date on channel beta
Flutter 2.2.0-10.3.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 06e2fd6357 (vor 11 Tagen) • 2021-05-08 11:28:22 -0700
Engine • revision a123e75c60
Tools • Dart 2.13.0 (build 2.13.0-211.14.beta)
flutter 医生也运行正常,哦,我在 linux 或 android 上也有同样的错误,所以它不是设备....
//future: bundle.loadString("assets/test.txt"),
future: bundle.loadString('AssetManifest.json'),
并得到:
flutter: {"assets/test.txt":["assets/test.txt"],"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
所以文件在那里???为什么我不能打开/得到它?
print(await rootBundle.loadString('AssetManifest.json'));
并且也因空异常而崩溃....
最佳答案
WidgetsFlutterBinding.ensureInitialized()
是解决我的问题以及@HannesHultergård 所写的关键。为了补充他的答案,主要应该是:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
print(await rootBundle.loadString("assets/test.txt"));
runApp(MyApp());
}
关于android - Flutter 无法从 Assets 中加载文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67604560/
我是一名优秀的程序员,十分优秀!