【发布时间】:2020-11-12 10:37:00
【问题描述】:
虽然它以前可以工作,但由于某种原因,我的代码现在已经停止工作。尽管它会获取所需的 json 数据,但构建不会呈现。我的应用页面上应该显示页面视图的错误是;
type String is not a subtype of 'Map<String,dynamic>'
但经过一些调整,现在错误是;
invalid arguments(s)
我想我可能已经把范围缩小到了未来;
FutureBuilder<List<SpeakContent>> futureStage() {
return new FutureBuilder<List<SpeakContent>>(
future: downloadJSON(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("Snapshot has data.");
List<SpeakContent> speakcrafts = snapshot.data;
return new CustomPageView(speakcrafts);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return new CircularProgressIndicator();
},
);
}
Future<List<SpeakContent>> downloadJSON() async {
final jsonEndpoint =
"http://example.com/getContent.php?";
final response = await get(jsonEndpoint);
if (response.statusCode == 200) {
List speakcrafts = json.decode(response.body);
debugPrint(speakcrafts.toString());
return speakcrafts
.map((speakcraft) => new SpeakContent.fromJson(speakcraft))
.toList();
} else
throw Exception('We were not able to successfully download the json data.');
}
虽然它没有抛出错误,但我注意到它没有在“if (snapshot.hasData)”行之后打印我的测试语句。
我不应该看到“快照有数据”吗?出现在我的 Android Studio 控制台中?
【问题讨论】:
-
可能是因为你的未来永远不会解决/永远不会返回。您可以在构建器函数或小部件/状态中向上移动未来,将其保存在像
final downloadFuture = downloadJSON()这样的变量中,然后使用future: downloadFuture,并添加downloadFuture.then回调以检查它是否真正解决。 -
你能显示方法
downloadJSON吗? -
根据要求,我刚刚编辑并包含了下载Json Claudio Redi。
标签: list flutter dart future snapshot