我是新来的,我想做下面的例子,但我得到了空值!!!
在有状态小部件和脚手架内部,我有一个按钮,其中有一个事件,例如:
onPressed: () {
Object json = {'xxx': xxx,};
Server_queries sq = Server_queries();
print(sq.post('register/', json).toString());
String holder = Server_queries().post('register/', json);
print(holder.toString());
////Both of them shows 'Null'
},
这是我的另一堂课:
import 'dart:convert';
import 'package:http/http.dart' as http;
const String server_address = 'xxxx';
class Server_queries {
String res;
String post(String link, Object json) {
var url = server_address + link;
var body = jsonEncode(json);
http
.post(url, headers: {"Content-Type": "application/json"}, body: body)
.then((http.Response response) {
if (response.statusCode == 201 || response.statusCode == 200) {
res = jsonDecode(response.body).toString();
} else {
res = response.statusCode.toString();
}
});
return res;
}
在 post 函数中打印 res 会显示响应,但问题存在于其他类中!!