【发布时间】:2021-04-05 09:01:20
【问题描述】:
我正在使用 Flutter / Dart 创建一个小应用程序,它运行良好。我的资产在pubspec.yaml 中正确定义。
但是,当我尝试从资产 (JSON) 加载数据时,它只加载了一半的数据。
appData.json
{
"plan": [
{
"name": "plan_demo",
"author": "A Name",
"version": 1,
"times_completed": 0,
"percent_done": 0,
"last_date_read": "1970-01-09",
"missedDate": [
{
"date": "1970-01-01"
},
{
"date": "1970-01-05"
}
],
"part": [
{
"isDone": false,
"startBook": 1,
"endBook": 1,
"startChapter": 1,
"endChapter": 1,
"startVerse": 1,
"endVerse": 5
},
{
"isDone": false,
"startBook": 1,
"endBook": 1,
"startChapter": 1,
"endChapter": 1,
"startVerse": 6,
"endVerse": 10
}
]
},
{
"name": "another plan",
"author": "Another Name",
"version": 1,
"times_completed": 0,
"percent_done": 0,
"last_date_read": "2020-12-28",
"missedDate": [
{
"date": "2020-11-01"
}
],
"part": [
{
"isDone": false,
"startBook": 1,
"endBook": 1,
"startChapter": 1,
"endChapter": 1,
"startVerse": 1,
"endVerse": 5
},
{
"isDone": false,
"startBook": 1,
"endBook": 1,
"startChapter": 1,
"endChapter": 1,
"startVerse": 6,
"endVerse": 10
}
]
}
]
}
调用方法如下:
@override
void initState() {
super.initState();
rootBundle.loadString("assets/appData.json").then((value) {
print('rootBundle : ' + value);
});
}
现在的输出是:
I/flutter (20595): rootBundle : {
I/flutter (20595): "plan": [
I/flutter (20595): {
I/flutter (20595): "name": "plan_demo",
I/flutter (20595): "author": "A Name",
I/flutter (20595): "version": 1,
I/flutter (20595): "times_completed": 0,
I/flutter (20595): "percent_done": 0,
I/flutter (20595): "last_date_read": "1970-01-09",
I/flutter (20595): "missedDate": [
I/flutter (20595): {
I/flutter (20595): "date": "1970-01-01"
I/flutter (20595): },
I/flutter (20595): {
I/flutter (20595): "date": "1970-01-05"
I/flutter (20595): }
I/flutter (20595): ],
I/flutter (20595): "part": [
I/flutter (20595): {
I/flutter (20595): "isDone": false,
I/flutter (20595): "startBook": 1,
I/flutter (20595): "endBook": 1,
I/flutter (20595): "startChapter": 1,
I/flutter (20595): "endChapter": 1,
I/flutter (20595): "startVerse": 1,
I/flutter (20595): "endVerse": 5
I/flutter (20595): },
I/flutter (20595): {
I/flutter (20595): "isDone": false,
I/flutter (20595): "startBook": 1,
I/flutter (20595): "endBook": 1,
I/flutter (20595): "startChapter": 1,
I/flutter (20595): "endChapter": 1,
I/flutter (20595): "startVerse":
为什么我的数据在文件中的某个点后被截断?
【问题讨论】:
-
打印语句只打印一定数量的字符......所以你的数据可能没问题。尝试将您的 json 存储在合适的变量中并打印其部分。
-
有点真实;在虚拟 Android 设备上运行时,数据很好,但从打印命令中删除。在 Edge 中运行时,打印命令会输出所有数据。这对我来说有点奇怪。
-
如果您的 json 长度为 1m 个字符,但不确定您是否想要全部打印出来。到目前为止,我对 print() 截断非常满意。 :-) 如果我需要更多,我通常使用一个类来定义它并准确打印我需要查看的位。