我希望我的 Scaffold 的 appBar 標題在啟動時顯示 Firebase 查詢中的專案總數(長度),但它一直回傳Instance of 'Future<int>'。我怎樣才能解決這個問題?
這是我的代碼:
Query itemList = FirebaseFirestore.instance
.collection('simple');
Future<int> itemCount() async => FirebaseFirestore.instance
.collection('simple')
.snapshots()
.length;
...
return Scaffold(
appBar: AppBar(
title: Text("# Items: " itemCount().toString()),
// DISPLAYS: # Items: Instance of 'Future<int>'
不幸的是,它顯示Instance of 'Future<int>'. 我必須更改什么才能獲得專案計數(長度)并在標題文本中顯示?
謝謝!
uj5u.com熱心網友回復:
您可以像這樣使用 FutureBuilder:
AppBar(
title: FutureBuilder<String>(
future: itemCount(),
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
return Text("# Items: " snapshot.data.toString());
}else {
Text("No data");
}
}),
)
uj5u.com熱心網友回復:
您正在呼叫“Future”函式,即該函式回傳一個 Future,因此您不能像那樣顯示它,您需要使用 await(如果您在異步函式中)或 a .then()(如果您不在異步函式中)。在您的情況下列印它的最佳方法是使用
浮動操作按鈕將專案添加到串列中,并且 onTap 從串列中洗掉選定的專案。標題保持更新。
我希望這對將來的某人有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529310.html
標籤:Google Cloud Collective 扑火力基地未来
上一篇:如何將選單帶到中心
