【问题标题】:Flutter Firebase document.id update problemFlutter Firebase document.id 更新问题
【发布时间】:2021-11-22 07:43:30
【问题描述】:

我在将“document.id”值发送到另一个页面时遇到问题。

我的第一页是这样的:

我想更新任务的标题和内容。当用户点击铅笔时,我导航到这个屏幕:

但是,在更新屏幕中,如您所见,“document.id”存在问题。要更新已经存在的文档,我需要 UpdateScreen 页面中的文档 ID,但我无法访问。我能做些什么来解决?

我的代码:

 return ListView(
            children: snapshot.data!.docs.map((DocumentSnapshot document) {
              Map<String, dynamic> data =
                  document.data()! as Map<String, dynamic>;
              return Card(
                child: ListTile(
                  title: Text(data['GorevBaslik']),
                  subtitle: Text(data['GorevIcerik']),
                  trailing: Wrap(
                    spacing: 12, // space between two icons
                    children: <Widget>[
                      IconButton(
                          icon: Icon(Icons.edit),
                          onPressed: () async {
                            await Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => UpdateStatusPage()));
                          }),
                      IconButton(
                        icon: Icon(Icons.remove),
                        onPressed: () async {
                          print(document.id);
                         // authService.removeStatus(document.id); //auth olarak değil status olarak değiştir service adını
                          },
                      ), // icon-2
                    ],
                  ),
                ),
              );
            }).toList(),
          );

以及更新页面:

return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Colors.white, //change your color here
        ),
        title: Text(
          "Görev Oluştur",
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.only(top: 120.0, left: 25.0, right: 25.0),
        child: Column(
          children: [
            TextFormField(
              controller: gorevBaslikController,
              decoration: InputDecoration(
                  prefixIcon: Icon(Icons.edit), labelText: "Güncellemek istediğiniz görev başlığı"),
            ),
            TextFormField(
              controller: gorevIcerikController,
              decoration: InputDecoration(
                  prefixIcon: Icon(Icons.article_rounded),
                  labelText: "Yeni görev içeriği"),
              maxLines: 3,
            ),
            SizedBox(height: 15),
            ElevatedButton(
              onPressed: (){
                //getStatus.addTask(gorevBaslikController.text, gorevIcerikController.text);
                firestore.collection('Task').doc('${auth.currentUser?.email}').collection('tasks').doc(document.id).update(
                  {
                    'GorevBaslik': gorevBaslikController,
                    'GorevIcerik': gorevIcerikController,
                  }
                );
              },
              child: Text(
                "Güncelle",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ],
        ),
      ),
    );

【问题讨论】:

    标签: firebase flutter firebase-realtime-database


    【解决方案1】:

    您应该在编辑文件中传递documentId or document 以更新正确的文档。

    UpdateScreen(@required this.documentID);
    

    从 UpdateScreen 的父屏幕传递 documentID。

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2020-09-05
      • 2020-05-24
      • 2021-02-01
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多