【发布时间】:2021-01-25 14:21:11
【问题描述】:
你好,我对 Flutter 和 Dart 很陌生 我想对我的数据库做点什么
我有这个代码来获取帖子
makePostRequest(int id, String type, String api) async {
final uri = 'url';
final headers = {'Content-Type': 'application/x-www-form-urlencoded'};
String body = "id=" + id.toString() + "&api=" + api + "&type=" + type;
final encoding = Encoding.getByName('utf-8');
Response response = await post(
uri,
headers: headers,
body: body,
encoding: encoding,
);
int statusCode = response.statusCode;
String responseBody = response.body;
print(statusCode);
//print(responseBody);
return responseBody;
}
在 initState 中我想把它拿出来放在我的小部件中,但我不知道该怎么做
Map<String, dynamic> reponseJson;
@override
void initState() {
makePostRequest(
widget.id, 'get-one', 'api_key')
.then((value) {
//print(value.toString());
String jsonTxt = value;
Map<String, dynamic> valueDBB = jsonDecode(jsonTxt);
//print(ValueDBB['name']);
reponseJson = valueDBB;
print(reponseJson);
return valueDBB;
//i get the value here
});
//but not here
super.initState();
}
请帮我多锁定 1 天
【问题讨论】: