我試圖從這樣的 api 獲取資料,但我無法顯示它我需要從請求 api 獲取資料并將其顯示在小部件中,api 給了我一個用戶的資料,我想像這樣顯示我的資料:
getUser() async {
var response = await http.get(Uri.parse(url));
var jsonData = jsonDecode(response.body);
return jsonData['data'].map<User>(User.fromJson).toString();
}
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(12))),
padding: EdgeInsets.only(left: 10, right: 10),
height: 120,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
CircleAvatar(
radius: 28,
backgroundImage: NetworkImage('API AVATAR'),
),
Text(
'TEXT FROM MY API',
style: TextStyle(fontSize: 17, color: Colors.black),
),
Icon(
Icons.arrow_forward_ios,
color: Colors.grey,
),
],
),
), ```
uj5u.com熱心網友回復:
作為在 Init 方法的 SetState 或 Build 中的快捷呼叫 Get Data 函式,但正確的方法是使用 Provider 包并在 Builder 中偵聽資料,通過實作 Change Notifier 類
uj5u.com熱心網友回復:
您將呼叫 api 的函式放在 initState 中,完成后您執行 setState。
您需要使用通用等待文本制作加載頁面。您制作的變數會在呼叫 API 時發生變化。
但是做你想做的最好的方法是使用 Future Builder。你為什么不想使用它?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/519584.html
標籤:扑api镖
