gpt4 book ai didi

json - Dart-HttpRequest.getString()返回JSON

转载 作者:行者123 更新时间:2023-12-03 02:57:08 25 4
gpt4 key购买 nike

我试图通过闭包从服务器路径(我将rpc用于API)中返回原始JSON,因为我想将其保留在同一函数中,而不是使用.then()并调用另一个。

这是我的代码:

GetEntryDiscussionsFromDb(){
var url = "http://localhost:8082/discussionApi/v1/getAllDiscussions";

getRawJson(String response){
return response;
}

HttpRequest.getString(url).then(getRawJson);
return getRawJson;
}

然后在我的主要职能,我这样做:
var getRawJson = GetEntryDiscussionsFromDb();
print(getRawJson());

通过这样做我得到此错误:
G.GetEntryDiscussionsFromDb(...).call$0 is not a function

我使用Closures错误吗?有没有办法在.then()内部返回实际的原始Json而不是调用另一个函数?

提前致谢

最佳答案

不确定您要完成什么。 return getRawJson;应该做什么?
getRawJson仅返回对getRawJson方法的引用。当您使用getRawJson

print(getRawJson());

调用不带参数的 getRawJson,但是它需要 String response。这就是为什么您收到错误消息的原因。

您无法避免使用 then。或者,您可以使用 async / await
GetEntryDiscussionsFromDb(){
var url = "http://localhost:8082/discussionApi/v1/getAllDiscussions";

getRawJson(String response){
return response;
}

return HttpRequest.getString(url).then(getRawJson);
}

主要
GetEntryDiscussionsFromDb()
.then(print);

要么
main() async {
...
var json = await GetEntryDiscussionsFromDb();
print(json);
}

关于json - Dart-HttpRequest.getString()返回JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320382/

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