如何使用 kotlin 数据类获取 Firestore 文档的文档 ID [英] How do I get the document ID for a Firestore document using kotlin data classes

查看:33
本文介绍了如何使用 kotlin 数据类获取 Firestore 文档的文档 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 kotlin 数据类

I have kotlin data class

data class Client(

    val name: String = "",
    val email: String = "",
    val phone: String ="") {
constructor():this("","","")}

我有 firestore 将数据填充到类中就好了,但是我在试图弄清楚如何将文档 ID 放入数据类而不必在文档本身中设置它时不知所措.这可能吗?

I have firestore populate the data into the class just fine, however I am at a loss trying to figure out how to get the document id into the data class without having to set it in the document itself. Is this possible?

推荐答案

是的,使用 DocumentSnapshot 可以在不存储的情况下获取 id.我将在这里尝试构建完整的示例.

Yes, it's possible to get id without storing it, using DocumentSnapshot. I will try to build complete examples here.

我创建了一个通用的 Model 类来保存 id:

I created a general Model class to hold the id:

@IgnoreExtraProperties
public class Model {
    @Exclude
    public String id;

    public <T extends Model> T withId(@NonNull final String id) {
        this.id = id;
        return (T) this;
    }
}

然后你可以用任何模型扩展它,不需要实现任何东西:

Then you extend it with any model, no need to implement anything:

public class Client extends Model

如果我在这里有客户列表,则尝试查询列表以仅获取 age == 20 的客户:

If I have list of clients here, trying to query the list to get only clients with age == 20:

clients.whereEqualTo("age", 20)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) {
                        // here you can get the id. 
                        Client client = document.toObject(client.class).withId(document.getId());
                       // you can apply your actions...
                    }
                } else {

                }
            }
        });

如果你使用EventListener,你也可以得到如下的id:

And if you are using EventListener, you can also get the id like the following:

clients.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
            // here you can get the id. 
            QueryDocumentSnapshot document = change.getDocument();
            Client client = document.toObject(client.class).withId(document.getId());
                       // you can apply your actions...
        }
    }
});

documentSnapshot.getId()) 将获取集合中 Document 的 id,而无需将 id 保存到文档中.

documentSnapshot.getId()) will get you the id of the Document in the collection without saving the id into the document.

使用模型不会让您编辑任何模型,并且不要忘记使用 @IgnoreExtraProperties

Using Model will not let you edit any of your models, and don't forget using @IgnoreExtraProperties

这篇关于如何使用 kotlin 数据类获取 Firestore 文档的文档 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆