由于所有檔案上的 for 回圈,我正在增加整個串列,但無法解決如何增加/減少特定索引,請告訴我該怎么做。我在流構建器和串列視圖構建器中有以下代碼。
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () async {
final QuerySnapshot result =
await FirebaseFirestore.instance
.collection('mynamazstatus')
.get();
final List<DocumentSnapshot> documents =
result.docs;
// ignore: avoid_print
for (var data in documents) {
FirebaseFirestore.instance
.collection('mynamazstatus')
.doc(data.id)
.update({
praises['count']: FieldValue.increment(1)
});
}
},
child: const Icon(
Icons.arrow_drop_up,
size: 20,
),
),
InkWell(
onTap: () async {
final QuerySnapshot result =
await FirebaseFirestore.instance
.collection('mynamazstatus')
.get();
final List<DocumentSnapshot> documents =
result.docs;
// ignore: avoid_print
for (var data in documents) {
FirebaseFirestore.instance
.collection('mynamazstatus')
.doc(data.id)
.update({
praises['count']: FieldValue.increment(-1)
});
}
},
child: const Icon(
Icons.arrow_drop_down,
size: 20,
),
),
],
),
它可以在應用程式 UI 和 firebase 中使用,但只需輕輕一按即可用于所有檔案。
uj5u.com熱心網友回復:
您可以從字面上添加一個名為index的欄位到您的檔案中,并將其用作獲取與該索引匹配的檔案的參考。假設您在從StreamBuilder小部件獲取所有檔案后在ListView.builder中呈現InkWell按鈕,如下所示:
StreamBuilder(
stream: FirebaseFirestore.instance.collection('mynamazstatus').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<DocumentSnapshot> docs = (snapshot.data! as QuerySnapshot).docs;
// return your documents in a list, for example
return ListView.builder(
itemCount: docs.length,
itemBuilder: (context, index) {
// ... use this index to both pull the document and to save to it as well
DocumentSnapshot docRef = docs.where((d) => d.data()['index'] == index).first;
// now, use the docRef to render your components, as well as
// hook it up to the InkWell
}
)
}
return Center(child: CircularProgressIndicator());
}
)
然后,當您在ListView.builder中構建小部件時,使用對按索引( docRef )獲取的DocumentSnapshot的參考,您可以這樣更新它:
InkWell(
onTap: () async {
FirebaseFirestore.instance
.collection('mynamazstatus')
.doc(docRef.id)
.update({
praises['count']: FieldValue.increment(-1)
});
},
child: const Icon(
Icons.arrow_drop_down,
size: 20,
),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441448.html
