Dart-HttpRequest.getString()返回JSON
我试图用闭包从我的服务器路径返回原始JSON(我使用rpc作为API),因为我希望将它保留在同一个函数中,而不是使用.then()并调用另一个函数 这是我的代码:Dart-HttpRequest.getString()返回JSON,json,api,server,dart,Json,Api,Server,Dart,我试图用闭包从我的服务器路径返回原始JSON(我使用rpc作为API),因为我希望将它保留在同一个函数中,而不是使用.then()并调用另一个函数 这是我的代码: GetEntryDiscussionsFromDb(){ var url = "http://localhost:8082/discussionApi/v1/getAllDiscussions"; getRawJson(String response){ return response; } HttpReq
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
我是否使用了错误的闭包?是否有一种方法可以返回.then()中的实际原始Json,而不是调用另一个函数
提前感谢我不知道你想做什么。什么是
返回getRawJson代码>应该怎么做
getRawJson
只返回对getRawJson
方法的引用。使用getRawJson
print(getRawJson());
调用不带参数的getRawJson
,但它需要字符串响应
。这就是您收到错误消息的原因
您无法避免使用然后。或者,您可以使用async
/wait
GetEntryDiscussionsFromDb(){
var url = "http://localhost:8082/discussionApi/v1/getAllDiscussions";
getRawJson(String response){
return response;
}
return HttpRequest.getString(url).then(getRawJson);
}
主要
或
哦,不知道我在从主数据库调用GetEntryDiscussionsFromDb时可以使用。。。谢谢:)then()。
GetEntryDiscussionsFromDb()
.then(print);
main() async {
...
var json = await GetEntryDiscussionsFromDb();
print(json);
}