gpt4 book ai didi

android - Flutter 无法从 Assets 中加载文本

转载 作者:行者123 更新时间:2023-12-04 23:52:32 25 4
gpt4 key购买 nike

我已阅读 https://flutter.dev/docs/development/ui/assets-and-images#asset-images-in-package-dependenciesFlutter - 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 ...
在 _MyHomePageState 类中我改变了:
  @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 上也有同样的错误,所以它不是设备....
所以我对如何完成这项工作一无所知,因为我的印象是遵循了文档和以前的帮助:(
顺便说一句,我认为 flutter 现在是空的,安全的......无论如何,我该如何让它工作?
提前致谢
[编辑]:甚至更陌生....我替换了
    //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"]}
所以文件在那里???为什么我不能打开/得到它?
[ed2]:好的,我不明白......我直接在 main 中尝试了这个:
 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/

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