在另一个 (onPressed) 函数中使用来自 Firebase 数据的 ListView 的键

Use Key from ListView of Firebase data in another (onPressed)-function

如您在我的代码中所见,我想将密钥(ListTile 的 docID)导出到函数 onPressedNegativ。有了它,我希望能够编辑我的 firestore 数据库的数据。这背后的确切想法是,将字段 ergebnis 更新为另一个值,即在我的 ListTile 的开头显示不同的图标。

所以问题是,我如何才能导出 ListTile 的正确 docID,我按下了按钮,将我带到了 onPressedNegativ 函数?

感谢您的帮助。

void onPressedNegativ() {
    // update something in firebase
    // how can I get the actual docID?
  }



  Widget _buildList(QuerySnapshot snapshot) {
    return ListView.separated(
      padding: EdgeInsets.all(20),
      separatorBuilder: (context, index) => Container(
        height: 10,
      ),
      itemCount: snapshot.docs.length,
      itemBuilder: (context, index) {
        final doc = snapshot.docs[index];
        return ListTile(
          key: Key(doc.id),
          leading: (doc["ergebnis"] == "Ausstehend")
              ? Icon(Icons.schedule)
              : Icon(Icons.check),
          title: Text(
            doc["nachname"] + ", " + doc["vorname"],
          ),
          subtitle: Text(doc["adresse"] +
              " / " +
              doc["Geburtsdatum"] +
              " / " +
              doc["telefon"]),
          trailing: Wrap(
            spacing: 12,
            children: [
              OutlineButton.icon(
                borderSide: BorderSide(color: Colors.red),
                onPressed: onPressedNegativ,
                icon: Icon(
                  Icons.cancel_rounded,
                  color: Colors.red,
                ),
                label: Text(
                  "Test positiv",
                  style: TextStyle(color: Colors.red),
                ),
                shape: RoundedRectangleBorder(
                    borderRadius: new BorderRadius.circular(30.0)),
              ),
              OutlineButton.icon(
                borderSide: BorderSide(color: Colors.green),
                onPressed: () {},
                icon: Icon(
                  Icons.check_box_rounded,
                  color: Colors.green,
                ),
                label: Text(
                  "Test negativ",
                  style: TextStyle(color: Colors.green),
                ),
                shape: RoundedRectangleBorder(
                    borderRadius: new BorderRadius.circular(30.0)),
              )
            ],
          ),
        );
      },
    );
  }
}
Void onPressedNegativ(String docID) {
    //Use your docID to do logic you want
    // Or update something in firebase
    print("got this ID from my button: $docID");
  }

当你想在按下时调用它:

onPressed: ()=> onPressedNegativ(doc.id),