【问题标题】:Use Key from ListView of Firebase data in another (onPressed)-function在另一个(onPressed)函数中使用来自 Firebase 数据的 ListView 的键
【发布时间】:2021-06-28 18:09:51
【问题描述】:

正如您在我的代码中看到的,我想将密钥(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)),
              )
            ],
          ),
        );
      },
    );
  }
}

【问题讨论】:

    标签: firebase flutter listview web


    【解决方案1】:
    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 调用它时:

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

    【讨论】:

    • 那行得通。非常感谢。和我想的一样容易。
    • 这并不难,你很快就会做更复杂的事情!如果您不介意将其标记为您问题的答案,我将不胜感激。
    • 已经试过了,3分钟后你的评论就是我问题的答案:)
    • 我的意思是通过在投票下标记 tik 来支持/接受它作为正确答案,谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 2021-11-02
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2019-06-27
    相关资源
    最近更新 更多