我想要做的是從 API 獲取 json 并從 json 內容中制作按鈕。
API回應沒有改變,所以我只想呼叫一次。(重新加載小部件時不需要呼叫)
所以,我的想法是簡單地從 initState 呼叫 API
Future<List> getSmartTags() async {
var url = Uri.parse("http://localhost:8008/api/smarttags/");
var resp = await http.get(url);
return json.decode(resp.body);
}
并想確認 json 是否正確回傳。
void initState() {
super.initState();
var temp = getSmartTags();
print(temp);
}
發生此錯誤。
[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'
#0 _SmartTagState.getSmartTags (package:flutter_aic/main.dart:64:17)
我認為這是基本概念,Future但我不明白。
uj5u.com熱心網友回復:
我認為Map<String, dynamic>每當您使用 json 解碼 html 正文時,您都會回傳 a,而不是List.
此外,您的回傳值不是 Future 因為await. 每當請求完成時,最終值都會存盤在您的變數resp中。
所以,理論上,它應該像這樣作業:
Map<String, dynamic> getSmartTags() async {
var url = Uri.parse("http://localhost:8008/api/smarttags/");
var resp = await http.get(url);
return json.decode(resp.body);
Future<Map<String, dynamic>>當您在沒有 . 的情況下撰寫它時,它將回傳 a wait。
并且為了避免由于您的異步函式和print()外部出現更多錯誤,我建議您在函式內部而不是 initState 中列印 JSON 輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478462.html
上一篇:如何在顫振中使用富文本的焦點?
