如何在 flutter 列表视图和 firebase 中动态更改值
how to change values dynamically in a flutter list view and firebase as well
由于所有文档上的 for 循环,我正在递增整个列表,但无法解决如何 increment/decrement 特定索引,请告诉我该怎么做。我在 streame 构建器和列表视图构建器中有以下代码。
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,但对于所有文档,只需轻轻一按。
您可以直接将一个名为 index 的字段添加到您的文档中,并将其用作引用以获取与该索引匹配的文档。假设您在从 StreamBuilder 小部件,如:
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 中构建小部件时,使用 [=16] 对获取的 DocumentSnapshot 的引用=]index (docRef) 你可以这样更新它:
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,
),
)
由于所有文档上的 for 循环,我正在递增整个列表,但无法解决如何 increment/decrement 特定索引,请告诉我该怎么做。我在 streame 构建器和列表视图构建器中有以下代码。
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,但对于所有文档,只需轻轻一按。
您可以直接将一个名为 index 的字段添加到您的文档中,并将其用作引用以获取与该索引匹配的文档。假设您在从 StreamBuilder 小部件,如:
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 中构建小部件时,使用 [=16] 对获取的 DocumentSnapshot 的引用=]index (docRef) 你可以这样更新它:
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,
),
)