我想根據 API 呼叫期間收到的 HTTP 代碼(200、400)顯示不同的圖示。在每個 ListTile 的開頭,我呼叫了異步的 iconExchange 函式。
我收到錯誤訊息:“無法將引數型別‘Future<Widget?>’分配給引數型別‘Widget?’。”
ListTile 位于名為“listParametres”的變數中的小說明。

class ApiExchangesPage extends StatelessWidget {
final User? user;
final DocumentSnapshot? member;
const ApiExchangesPage({Key? key, required this.user, this.member})
: super(key: key);
@override
Widget build(BuildContext context) {
final listParametres = [
Column(children: [
ListTile(
onTap: () {
print(ftx.titleDatabase);
Navigator.push(context, MaterialPageRoute(builder: (_) {
return ExchangeConnectScreen(user: user, exchange: ftx);
}));
},
leading: iconExchange(ftx),
title: Text(
"FTX",
style: TextStyle(color: Colors.white),
),
trailing: Icon(
Icons.arrow_right,
color: Colors.white,
),
),
ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return ExchangeConnectScreen(
user: user,
exchange: binance,
);
}));
},
leading: iconExchange(binance),
title: Text(
"Binance Future",
style: TextStyle(color: Colors.white),
),
trailing: Icon(
Icons.arrow_right,
color: Colors.white,
),
),
ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return ExchangeConnectScreen(
user: user,
exchange: bybit,
);
}));
},
leading: iconExchange(bybit),
title: Text(
"Bybit",
style: TextStyle(color: Colors.white),
),
trailing: Icon(
Icons.arrow_right,
color: Colors.white,
),
),
ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return ExchangeConnectScreen(
user: user,
exchange: bitmex,
);
}));
},
leading: iconExchange(bitmex),
title: Text(
"Bitmex",
style: TextStyle(color: Colors.white),
),
trailing: Icon(
Icons.arrow_right,
color: Colors.white,
),
),
])
];
return Scaffold(
backgroundColor: backgroundColor,
appBar: MyAppBar(
title: "Exchanges",
),
body: ListView.separated(
itemBuilder: (context, index) {
return listParametres[index];
},
separatorBuilder: (context, index) {
return Divider(
color: Colors.white,
);
},
itemCount: listParametres.length),
);
}
Future<int> appelBDD(Exchange exchange) async {
var member = await FirebaseFirestore.instance
.collection('member')
.doc(user?.uid)
.get();
print(member);
if (member.data()![exchange] != null) {
List pa = await member.data()?[exchange] ?? [""];
int code = await TestApiClass()
.appelApi(exchange, pa[0].toString(), pa[1].toString());
print(code);
return code;
}
return 0;
}
Future<Widget?> iconExchange(Exchange exchange) async {
var appel = await appelBDD(exchange);
print(exchange.titleDatabase);
print(appel);
if (appel == 200) {
return Icon(
Icons.check_circle,
color: Colors.green,
);
} else {
return Icon(
Icons.dangerous_rounded,
color: Colors.red,
);
}
}
}
預先感謝您的幫助。
uj5u.com熱心網友回復:
您應該使用專用FutureBuilder小部件:https : //api.flutter.dev/flutter/widgets/FutureBuilder-class.html
這樣你就可以有一個不同的圖示:
- 發出請求時(加載)
- 請求失敗
- 請求成功
使用示例:
FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362189.html
標籤:扑
